20260623

【Applescript OBJC】Finderラベルをつける

【Applescript OBJC】Finderラベルをつける

NOTE記事一覧ですnote.com

【Safari・FireFox用Script Editorで開く】 |

AddLabel.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005Finder ラベル番号 OBJC用 AS用のラベル番号と逆順です
0060:ラベル無し
0071:グレー
0082:グリーン
0093:パープル
0104:ブルー
0115:イエロー
0126:レッド
0137:オレンジ 
014
015Finderラベル設定
016フォルダ ファイル両用ダイアログ
017OBJC版なので
018macOS26からのフォルダカスタマイズに対応しています
019
020ラベル無しを選択した場合『タグ』は削除されません。
021あくまでもラベル番号を設定します
022
023
024v1 初回作成
025
026com.cocolog-nifty.quicktimer.icefloe *)
027----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
028use AppleScript version "2.8"
029use framework "Foundation"
030use framework "AppKit"
031use scripting additions
032property listAliasFilePath : {} as list
033property refMe : a reference to current application
034
035########################
036# RUN
037#   on run {listAliasFilePath}
038
039on run
040   
041   if (count of listAliasFilePath) = 0 then
042      ########################
043      #ダイアログ
044      set strMsg to ("ファイル 選択") as text
045      set strPrompt to ("ファイル を選択してください" & return & "複数選択可" & return & "フォルダの場合はここをキャンセルで次のダイアログでフォルダ選択出来ます") as text
046      set aliasDesktopDirPath to (path to desktop from user domain) as alias
047      set listUTI to {"public.item"} as list
048      try
049         tell application "Finder"
050            tell application "SystemUIServer"
051               activate
052               set listAliasFilePath to (choose file strMsg with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and multiple selections allowed without showing package contents) as list
053            end tell
054         end tell
055      on error strErrMes number numErrNo
056         log strErrMes & numErrNo
057         set strMsg to ("フォルダ 選択") as text
058         set strPrompt to ("フォルダ を選択してください" & return & "複数選択可") as text
059         tell application "Finder"
060            try
061               tell application "SystemUIServer"
062                  activate
063                  set listAliasFilePath to (choose folder strMsg with prompt strPrompt default location aliasDesktopDirPath with invisibles and multiple selections allowed without showing package contents) as list
064               end tell
065            on error strErrMes number numErrNo
066               log strErrMes & numErrNo
067               return false
068            end try
069         end tell
070      end try
071      if listAliasFilePath is {} then
072         return false
073      end if
074   end if
075   ########################
076   #OPENへ渡す
077   set boolDone to (open listAliasFilePath)
078   
079   
080   set listAliasFilePath to {} as list
081   try
082      with timeout of 3 seconds
083         tell application "System Events" to quit
084      end timeout
085   on error strErrMes number numErrNo
086      log "Quit System Events" & strErrMes & numErrNo
087      return false
088   end try
089   
090   return boolDone
091end run
092
093########################
094# OPEN
095on open listAliasFilePath
096   
097   
098   ################################
099   #ダイアログ
100   set listLabel to {"ラベル削除", "オレンジ", "レッド", "イエロー", "ブルー", "パープル", "グリーン", "グレイ"} as list
101   
102   set strTitle to ("選んでください") as text
103   set strPrompt to ("ひとつ選んでください") as text
104   set strOK to ("OK") as text
105   set strCancel to ("キャンセル") as text
106   try
107      tell application "System Events"
108         activate
109         set valueResponse to (choose from list listLabel with title strTitle with prompt strPrompt default items (last item of listLabel) OK button name strOK cancel button name strCancel with empty selection allowed without multiple selections allowed)
110      end tell
111   on error strErrMes number numErrNo
112      log strErrMes & numErrNo
113      return doQuitSystemEvents()
114   end try
115   if (class of valueResponse) is boolean then
116      log "ユーザによってキャンセルされました。"
117      return doQuitSystemEvents()
118   else if (class of valueResponse) is list then
119      if valueResponse is {} then
120         log "Error 何も選んでいません"
121         return doQuitSystemEvents()
122      else
123         set strResponse to (first item of valueResponse) as text
124      end if
125   end if
126   
127   ################################
128   #リストの何番目?サブ
129   set numListNO to doOffsetInList(strResponse, listLabel) as integer
130   set numSetLabelNo to (numListNO - 1) as integer
131   if numSetLabelNo0 then
132      set numSetLabelNo to (8 - numSetLabelNo) as integer
133   end if
134   
135   #本処理に回す
136   set boolDone to doAliasList2Job(listAliasFilePath, numSetLabelNo, strResponse)
137   
138   return true
139end open
140
141################################
142#本処理
143on doAliasList2Job(arglistAliasFilePath, argNumSetLabelNo, argStrResponse)
144   
145   set numSetLabelNo to argNumSetLabelNo as integer
146   set strResponse to argStrResponse as text
147   tell application "Finder"
148      set listAliasFilePath to arglistAliasFilePath as alias list
149   end tell
150   
151   repeat with itemAliasFilePath in listAliasFilePath
152      #パス
153      set aliasItemFilePath to itemAliasFilePath as alias
154      set strFilePath to (POSIX path of aliasItemFilePath) as text
155      set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
156      set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping()
157      set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
158      set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath()
159      set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false))
160      #現在設定されているタグを取得して
161      set ocidKey to (refMe's NSURLTagNamesKey)
162      set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(ocidKey) |error|:(reference))
163      set ocidTagNameArray to (second item of listResponse)
164      if ocidTagNameArray = (missing value) then
165         set ocidTagNameArrayM to refMe's NSMutableArray's alloc()'s init()
166      else
167         set ocidTagNameArrayM to (second item of listResponse)'s mutableCopy()
168      end if
169      (ocidTagNameArrayM's addObject:(strResponse))
170      set listDone to (ocidFilePathURL's setResourceValue:(ocidTagNameArrayM) forKey:(ocidKey) |error|:(reference))
171      
172      
173      #ラベル番号を設定
174      set ocidLabelNo to (refMe's NSNumber's numberWithInteger:(numSetLabelNo))
175      set ocidKey to (refMe's NSURLLabelNumberKey)
176      set listResult to (ocidFilePathURL's setResourceValue:(ocidLabelNo) forKey:(ocidKey) |error|:(reference))
177      set boolDone to (first item of listResult) as boolean
178      
179      
180      tell application "Finder"
181         update aliasItemFilePath
182      end tell
183   end repeat
184   return boolDone
185   
186end doAliasList2Job
187
188########################
189#System Eventsを終了させる
190on doQuitSystemEvents()
191   try
192      with timeout of 3 seconds
193         tell application "System Events" to quit
194      end timeout
195   on error strErrMes number numErrNo
196      log "Quit System Events" & strErrMes & numErrNo
197      return false
198   end try
199   return true
200end doQuitSystemEvents
201
202################################
203#リストの何番目?サブ
204to doOffsetInList(argText, argList)
205   set numCntPosition to 1 as integer
206   repeat with itemChooser in argList
207      set strItemChooser to itemChooser as text
208      if strItemChooser is argText then
209         set numCntOffset to numCntPosition as integer
210      end if
211      set numCntPosition to (numCntPosition + 1) as integer
212   end repeat
213   return numCntOffset
214end doOffsetInList
AppleScriptで生成しました