[sfl3] decodeObjectOfClassで解凍する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # LSSharedFileList を Plistに解凍します |
005 | # initForReadingFromDataで読み込んで |
006 | # decodeObjectOfClassで解凍します |
007 | #com.cocolog-nifty.quicktimer.icefloe |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | use AppleScript version "2.8" |
010 | use framework "Foundation" |
011 | use framework "AppKit" |
012 | use scripting additions |
013 | property refMe : a reference to current application |
014 | |
015 | set appFileManager to refMe's NSFileManager's defaultManager() |
016 | |
017 | ################################### |
018 | #####ファイル選択ダイアログ |
019 | ################################### |
020 | set appFileManager to refMe's NSFileManager's defaultManager() |
021 | set ocidURLArray to appFileManager's URLsForDirectory:(refMe's NSApplicationSupportDirectory) inDomains:(refMe's NSUserDomainMask) |
022 | set ocidAppSuppDirPathURL to ocidURLArray's firstObject() |
023 | set strAppendPath to ("com.apple.sharedfilelist") as text |
024 | set ocidDefaultLocationURL to ocidAppSuppDirPathURL's URLByAppendingPathComponent:(strAppendPath) isDirectory:true |
025 | set aliasDefaultLocation to (ocidDefaultLocationURL's absoluteURL()) as alias |
026 | tell current application |
027 | set strName to name as text |
028 | end tell |
029 | ####スクリプトメニューから実行したら |
030 | if strName is "osascript" then |
031 | tell application "Finder" |
032 | activate |
033 | end tell |
034 | else |
035 | tell current application |
036 | activate |
037 | end tell |
038 | end if |
039 | ####ダイアログを出す |
040 | set listUTI to {"public.item", "dyn.ah62d4rv4ge81g3xqgk", "dyn.ah62d4rv4ge81g3xqgq", "dyn.ah62d4rv4ge80e7dr"} as list |
041 | set aliasFilePath to (choose file with prompt "sfl3ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
042 | ####入力ファイルパス |
043 | set strFilePath to POSIX path of aliasFilePath |
044 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
045 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
046 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
047 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
048 | set ocidPrefixName to ocidBaseFilePathURL's lastPathComponent() |
049 | ################################### |
050 | #####保存先ダイアログ |
051 | ################################### |
052 | ###ファイル名 |
053 | set strPrefixName to ocidPrefixName as text |
054 | ###拡張子変える場合 |
055 | set strFileExtension to "plist" |
056 | ###ダイアログに出すファイル名 |
057 | set strDefaultName to (strPrefixName & "." & strFileExtension) as text |
058 | set strPromptText to "名前を決めてください" |
059 | set strMesText to "名前を決めてください" |
060 | #### |
061 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
062 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
063 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
064 | |
065 | ####実在しない『はず』なのでas «class furl»で |
066 | set aliasSaveFilePath to (choose file name strMesText default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl» |
067 | ####UNIXパス |
068 | set strSaveFilePath to POSIX path of aliasSaveFilePath as text |
069 | ####ドキュメントのパスをNSString |
070 | set ocidSaveFilePath to refMe's NSString's stringWithString:strSaveFilePath |
071 | ####ドキュメントのパスをNSURLに |
072 | set ocidSaveFilePathURL to refMe's NSURL's fileURLWithPath:ocidSaveFilePath |
073 | ###拡張子取得 |
074 | set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text |
075 | ###ダイアログで拡張子を取っちゃった時対策 |
076 | if strFileExtensionName is not strFileExtension then |
077 | set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:(strFileExtension) |
078 | end if |
079 | |
080 | ################################### |
081 | #####本処理 |
082 | ################################### |
083 | # NSDataに読み込んで |
084 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
085 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference) |
086 | if (item 2 of listResponse) = (missing value) then |
087 | log "正常処理" |
088 | set ocidReadData to (item 1 of listResponse) |
089 | else if (item 2 of listResponse) ≠ (missing value) then |
090 | log (item 2 of listResponse)'s code() as text |
091 | log (item 2 of listResponse)'s localizedDescription() as text |
092 | return "エラーしました" |
093 | end if |
094 | |
095 | # NSKeyedUnarchiverに読み込んで |
096 | set listDone to refMe's NSKeyedUnarchiver's alloc()'s initForReadingFromData:(ocidReadData) |error| :(reference) |
097 | |
098 | log className() of (item 1 of listDone) as text |
099 | if (item 2 of listDone) = (missing value) then |
100 | log "initForReadingFromData 正常処理" |
101 | set ocidUnarchiverData to (item 1 of listDone) |
102 | else if (item 2 of listDone) ≠ (missing value) then |
103 | log (item 2 of listDone)'s code() as text |
104 | log (item 2 of listDone)'s localizedDescription() as text |
105 | return "initForReadingFromData エラーしました" |
106 | end if |
107 | |
108 | ##decodeObjectOfClassで解凍する |
109 | set ocidPlistDict to ocidUnarchiverData's decodeObjectOfClass:(refMe's NSObject's class) forKey:(refMe's NSKeyedArchiveRootObjectKey) |
110 | |
111 | # 解凍したデータ=DICTをPLISTで保存する |
112 | #XML形式 |
113 | set ocidFromat to refMe's NSPropertyListXMLFormat_v1_0 |
114 | #NSPropertyListSerialization |
115 | set listResponst to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFromat) options:0 |error| :(reference) |
116 | set ocidPlistEditData to (item 1 of listResponst) |
117 | #ファイル保存 |
118 | set listDone to ocidPlistEditData's writeToURL:(ocidSaveFilePathURL) options:0 |error| :(reference) |
119 | |
120 | if (item 1 of listDone) is true then |
121 | log "正常処理" |
122 | else if (item 2 of listDone) ≠ (missing value) then |
123 | log (item 2 of listDone)'s code() as text |
124 | log (item 2 of listDone)'s localizedDescription() as text |
125 | return "エラーしました" |
126 | end if |
AppleScriptで生成しました |
| 固定リンク