[xattr]フォルダにタグを付与する(sudo対応版)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | # xattrコマンドを使ってタグをセットします |
005 | # |
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 | |
015 | #com.apple.LSSharedFileList.ProjectsItems.sfl3を読み込む |
016 | set appFileManager to refMe's NSFileManager's defaultManager() |
017 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationSupportDirectory) inDomains:(refMe's NSUserDomainMask)) |
018 | set ocidApplicatioocidupportDirPathURL to ocidURLsArray's firstObject() |
019 | set ocidSfl3FilePathURL to ocidApplicatioocidupportDirPathURL's URLByAppendingPathComponent:("com.apple.sharedfilelist/com.apple.LSSharedFileList.ProjectsItems.sfl3") isDirectory:(false) |
020 | # NSDataに読み込んで |
021 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
022 | set listResponse to refMe's NSData's dataWithContentsOfURL:(ocidSfl3FilePathURL) options:(ocidOption) |error| :(reference) |
023 | if (item 2 of listResponse) is (missing value) then |
024 | set ocidReadData to (item 1 of listResponse) |
025 | log "dataWithContentsOfURL 正常終了" |
026 | else |
027 | log (item 2 of listResponse)'s code() as text |
028 | log (item 2 of listResponse)'s localizedDescription() as text |
029 | return "dataWithContentsOfURL 失敗しました" |
030 | end if |
031 | # unarchivedObjectOfClassで解凍する |
032 | #DATAを解凍する |
033 | set ocidClassListArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
034 | (ocidClassListArray's addObject:(refMe's NSDictionary's class)) |
035 | (ocidClassListArray's addObject:(refMe's NSMutableDictionary's class)) |
036 | (ocidClassListArray's addObject:(refMe's NSArray's class)) |
037 | (ocidClassListArray's addObject:(refMe's NSMutableArray's class)) |
038 | (ocidClassListArray's addObject:(refMe's NSObject's classForKeyedUnarchiver)) |
039 | (ocidClassListArray's addObject:(refMe's NSObject's class)) |
040 | #クラスセット |
041 | set ocidSetClass to refMe's NSSet's alloc()'s initWithArray:(ocidClassListArray) |
042 | #解凍 |
043 | set listResponse to refMe's NSKeyedUnarchiver's unarchivedObjectOfClasses:(ocidSetClass) fromData:(ocidReadData) |error| :(reference) |
044 | if (item 2 of listResponse) = (missing value) then |
045 | log "unarchivedObjectOfClasses 正常処理" |
046 | set ocidReadDict to (item 1 of listResponse) |
047 | else if (item 2 of listResponse) ≠ (missing value) then |
048 | log (item 2 of listResponse)'s code() as text |
049 | log (item 2 of listResponse)'s localizedDescription() as text |
050 | return "unarchivedObjectOfClasses エラーしました" |
051 | end if |
052 | ####### |
053 | #ダイアログに渡すArray |
054 | set ocidItemNameArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
055 | ####### |
056 | #現在登録済みのタグ名を取得する |
057 | set ocidItemsArray to ocidReadDict's objectForKey:("items") |
058 | repeat with itemDict in ocidItemsArray |
059 | set ocidName to (itemDict's valueForKey:("Name")) |
060 | (ocidItemNameArray's addObject:(ocidName)) |
061 | end repeat |
062 | set listItemsArray to ocidItemNameArray as list |
063 | try |
064 | ############################## |
065 | ###ダイアログを前面に出す |
066 | set strName to (name of current application) as text |
067 | if strName is "osascript" then |
068 | tell application "Finder" to activate |
069 | else |
070 | tell current application to activate |
071 | end if |
072 | ### |
073 | set strTitle to "選んでください" as text |
074 | set strPrompt to "設定するタグを選んでください" as text |
075 | set listResponse to (choose from list listItemsArray with title strTitle with prompt strPrompt default items (item 1 of listItemsArray) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) as list |
076 | on error |
077 | log "エラーしました" |
078 | return "エラーしました" |
079 | error "エラーしました" number -200 |
080 | end try |
081 | if listResponse = {} then |
082 | return "何も選択していない" |
083 | else if (item 1 of listResponse) is false then |
084 | return "キャンセルしました" |
085 | error "キャンセルしました" number -200 |
086 | end if |
087 | #####戻り値でPLISTを作っておく |
088 | #PLISTになるArray |
089 | set ocidLabelArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
090 | #戻り値の数だけタグを設定する |
091 | repeat with itemResponse in listResponse |
092 | set ocidSetValue to (refMe's NSString's stringWithString:(itemResponse)) |
093 | (ocidLabelArray's addObject:(ocidSetValue)) |
094 | end repeat |
095 | ###NSDATAのPLISTに変換 データはXML形式 |
096 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
097 | #set ocidFormat to (refMe's NSPropertyListBinaryFormat_v1_0) |
098 | set ocidPlistSerial to (refMe's NSPropertyListSerialization) |
099 | set ocidOption to (refMe's NSPropertyListMutableContainersAndLeaves) |
100 | set listResponse to ocidPlistSerial's dataWithPropertyList:(ocidLabelArray) format:(ocidFormat) options:(ocidOption) |error| :(reference) |
101 | if (item 2 of listResponse) = (missing value) then |
102 | log "正常処理" |
103 | set ocidPlistData to (item 1 of listResponse) |
104 | else if (item 2 of listResponse) ≠ (missing value) then |
105 | log (item 2 of listResponse)'s code() as text |
106 | log (item 2 of listResponse)'s localizedDescription() as text |
107 | return "エラーしました" |
108 | end if |
109 | ###テキストに |
110 | set ocidPlistString to refMe's NSMutableString's alloc()'s initWithData:(ocidPlistData) encoding:(refMe's NSUTF8StringEncoding) |
111 | set strPlistString to ocidPlistString as text |
112 | ### |
113 | set strName to (name of current application) as text |
114 | if strName is "osascript" then |
115 | tell application "Finder" to activate |
116 | else |
117 | tell current application to activate |
118 | end if |
119 | set aliasDefaultLocation to (path to desktop from user domain) as alias |
120 | set strPromptText to "フォルダをえらんでください" |
121 | set strMesText to "フォルダをえらんでください" |
122 | try |
123 | set listChoosePath to (choose folder strMesText with prompt strPromptText default location aliasDefaultLocation with multiple selections allowed, invisibles and showing package contents) as list |
124 | on error |
125 | log "エラーしました" |
126 | return "エラーしました" |
127 | end try |
128 | |
129 | repeat with itemChoosePath in listChoosePath |
130 | set aliasChoosePath to itemChoosePath as alias |
131 | set strDropPath to (POSIX path of aliasChoosePath) as text |
132 | |
133 | set strCommandText to ("/usr/bin/xattr -w 'com.apple.metadata:_kMDItemUserTags' '" & ocidPlistString & "' \"" & strDropPath & "\"") |
134 | log strCommandText |
135 | try |
136 | do shell script strCommandText |
137 | on error |
138 | try |
139 | set strCommandText to ("/usr/bin/sudo /usr/bin/xattr -w 'com.apple.metadata:_kMDItemUserTags' '" & ocidPlistString & "' \"" & strDropPath & "\"") |
140 | log strCommandText |
141 | end try |
142 | end try |
143 | end repeat |
144 | |
AppleScriptで生成しました |
| 固定リンク