[NSData]OPENから保存まで
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # |
005 | # com.cocolog-nifty.quicktimer.icefloe |
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
007 | use AppleScript version "2.8" |
008 | use framework "Foundation" |
009 | use framework "AppKit" |
010 | use scripting additions |
011 | |
012 | property refMe : a reference to current application |
013 | |
014 | ##画像のURL |
015 | set strURL to ("https://abs.twimg.com/favicons/twitter.2.ico") as text |
016 | set ocidURLString to refMe's NSString's stringWithString:(strURL) |
017 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString) |
018 | set ocidFileName to ocidURL's lastPathComponent() |
019 | |
020 | ##NSDATAに読み込む |
021 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
022 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference) |
023 | if (item 2 of listResponse) = (missing value) then |
024 | log "正常処理" |
025 | set ocidReadData to (item 1 of listResponse) |
026 | else if (item 2 of listResponse) ≠ (missing value) then |
027 | set strErrorNO to (item 2 of listResponse)'s code() as text |
028 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
029 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
030 | return "エラーしました" & strErrorNO & strErrorMes |
031 | end if |
032 | |
033 | #保存用のパスを作っておく |
034 | set appFileManager to refMe's NSFileManager's defaultManager() |
035 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
036 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
037 | set ocidSaveFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false) |
038 | |
039 | ##NSDataで保存 |
040 | set ocidOption to (refMe's NSDataWritingAtomic) |
041 | set listDone to ocidReadData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference) |
042 | if (item 1 of listDone) is true then |
043 | log "正常処理" |
044 | else if (item 2 of listDone) ≠ (missing value) then |
045 | set strErrorNO to (item 2 of listDone)'s code() as text |
046 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
047 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
048 | return "エラーしました" & strErrorNO & strErrorMes |
049 | end if |
AppleScriptで生成しました |
| 固定リンク