20260623

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

Applescript式:Finderラベル番号を設定する

NOTE記事一覧ですnote.com
 

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

AddLabel_AS.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005Finder ラベル番号
0061:オレンジ
0072:レッド
0083:イエロー
0094:ブルー
0105:パープル
0116:グリーン
0127:グレー
013
014Finderラベル設定
015フォルダ ファイル両用ダイアログ
016クラシックなFinderラベルですので
017macOS26のフォルダカスタマイズには対応していません。
018
019
020v1 初回作成
021
022com.cocolog-nifty.quicktimer.icefloe *)
023----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
024use AppleScript version "2.8"
025use scripting additions
026property listAliasFilePath : {} as list
027
028
029########################
030# RUN
031#   on run {listAliasFilePath}
032
033on run
034   
035   if (count of listAliasFilePath) = 0 then
036      ########################
037      #ダイアログ
038      set strMsg to ("ファイル 選択") as text
039      set strPrompt to ("ファイル を選択してください" & return & "複数選択可" & return & "フォルダの場合はここをキャンセルで次のダイアログでフォルダ選択出来ます") as text
040      set aliasDesktopDirPath to (path to desktop from user domain) as alias
041      set listUTI to {"public.item"} as list
042      try
043         tell application "Finder"
044            tell application "SystemUIServer"
045               activate
046               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
047            end tell
048         end tell
049      on error strErrMes number numErrNo
050         log strErrMes & numErrNo
051         set strMsg to ("フォルダ 選択") as text
052         set strPrompt to ("フォルダ を選択してください" & return & "複数選択可") as text
053         tell application "Finder"
054            try
055               tell application "SystemUIServer"
056                  activate
057                  set listAliasFilePath to (choose folder strMsg with prompt strPrompt default location aliasDesktopDirPath with invisibles and multiple selections allowed without showing package contents) as list
058               end tell
059            on error strErrMes number numErrNo
060               log strErrMes & numErrNo
061               return false
062            end try
063         end tell
064      end try
065      if listAliasFilePath is {} then
066         return false
067      end if
068   end if
069   ########################
070   #OPENへ渡す
071   set boolDone to (open listAliasFilePath)
072   
073   
074   set listAliasFilePath to {} as list
075   try
076      with timeout of 3 seconds
077         tell application "System Events" to quit
078      end timeout
079   on error strErrMes number numErrNo
080      log "Quit System Events" & strErrMes & numErrNo
081      return false
082   end try
083   
084   return boolDone
085end run
086
087########################
088# OPEN
089on open listAliasFilePath
090   
091   
092   ################################
093   #ダイアログ
094   set listLabel to {"ラベル削除", "オレンジ", "レッド", "イエロー", "ブルー", "パープル", "グリーン", "グレイ"} as list
095   
096   set strTitle to ("選んでください") as text
097   set strPrompt to ("ひとつ選んでください") as text
098   set strOK to ("OK") as text
099   set strCancel to ("キャンセル") as text
100   try
101      tell application "System Events"
102         activate
103         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)
104      end tell
105   on error strErrMes number numErrNo
106      log strErrMes & numErrNo
107      return doQuitSystemEvents()
108   end try
109   if (class of valueResponse) is boolean then
110      log "ユーザによってキャンセルされました。"
111      return doQuitSystemEvents()
112   else if (class of valueResponse) is list then
113      if valueResponse is {} then
114         log "Error 何も選んでいません"
115         return doQuitSystemEvents()
116      else
117         set strResponse to (first item of valueResponse) as text
118      end if
119   end if
120   ################################
121   #リストの何番目?サブ
122   set numListNO to doOffsetInList(strResponse, listLabel) as integer
123   set numSetLabelNo to (numListNO - 1) as integer
124   #本処理に回す
125   set boolDone to doAliasList2Job(listAliasFilePath, numSetLabelNo)
126   
127   return true
128end open
129
130################################
131#本処理
132on doAliasList2Job(arglistAliasFilePath, argNumSetLabelNo)
133   
134   set numSetLabelNo to argNumSetLabelNo as integer
135   tell application "Finder"
136      set listAliasFilePath to arglistAliasFilePath as alias list
137   end tell
138   
139   repeat with itemAliasFilePath in listAliasFilePath
140      
141      
142      set aliasItemFilePath to itemAliasFilePath as alias
143      set recordInfoFor to (info for aliasItemFilePath) as record
144      set boolIsDir to (folder of recordInfoFor) as boolean
145      if boolIsDir is false then
146         set boolDone to doSetFinderLabel(aliasItemFilePath, numSetLabelNo)
147      else if boolIsDir is true then
148         set boolDone to doSetDirFinderLabel(aliasItemFilePath, numSetLabelNo)
149      end if
150      tell application "Finder"
151         update aliasItemFilePath
152      end tell
153   end repeat
154   
155   
156end doAliasList2Job
157
158########################
159#System Eventsを終了させる
160on doQuitSystemEvents()
161   try
162      with timeout of 3 seconds
163         tell application "System Events" to quit
164      end timeout
165   on error strErrMes number numErrNo
166      log "Quit System Events" & strErrMes & numErrNo
167      return false
168   end try
169   return true
170end doQuitSystemEvents
171
172########################
173#ラベルをつける ファイル
174on doSetFinderLabel(argAliasFilePath, argNumSetLabelNo)
175   set numSetLabelNo to argNumSetLabelNo as integer
176   set aliasFilePath to argAliasFilePath as alias
177   try
178      tell application "Finder"
179         tell file aliasFilePath
180            set label index to numSetLabelNo
181         end tell
182      end tell
183   on error strErrMsg number numErrNo
184      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
185      return false
186   end try
187   return true
188   
189end doSetFinderLabel
190
191########################
192#ラベルをつける フォルダ
193on doSetDirFinderLabel(argAliasFilePath, argNumSetLabelNo)
194   set numSetLabelNo to argNumSetLabelNo as integer
195   set aliasFilePath to argAliasFilePath as alias
196   try
197      tell application "Finder"
198         tell folder aliasFilePath
199            set label index to numSetLabelNo
200         end tell
201      end tell
202   on error strErrMsg number numErrNo
203      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
204      return false
205   end try
206   return true
207   
208end doSetDirFinderLabel
209
210################################
211#リストの何番目?サブ
212to doOffsetInList(argText, argList)
213   set numCntPosition to 1 as integer
214   repeat with itemChooser in argList
215      set strItemChooser to itemChooser as text
216      if strItemChooser is argText then
217         set numCntOffset to numCntPosition as integer
218      end if
219      set numCntPosition to (numCntPosition + 1) as integer
220   end repeat
221   return numCntOffset
222end doOffsetInList
AppleScriptで生成しました