I have written a script to label with the color labels files of a chosen type within a chosen directory. It runs, but it takes so long, it times out. I put a timeout for four hours in the code, but that is still not long enough when working with a directory like my documents folder which has a lot of files. Plus it ties up Finder for all the time it is running. Is there a better way to do this without the program taking forever…?
on run
set theFolder to choose folder
display dialog “What type of file would you like to label?” default answer “”
set fileextension to text returned of result
set labelnumber to picklabelcolor()
with timeout of (60 * 60 * 4) seconds
tell application “Finder”
ignoring case
set theFiles to every file of entire contents of theFolder whose name extension is fileextension
end ignoring
repeat with aFile in theFiles
set label index of aFile to labelnumber --or 2 or 3, etc.
end repeat
end tell
end timeout
end run
on picklabelcolor()
set listofcolors to {“Orange”, “Red”, “Yellow”, “Blue”, “Purple”, “Green”, “Gray”}
set thecolor to (choose from list listofcolors with title “Possible Colors” with prompt “Choose A Label Color”) as text
if thecolor is not in listofcolors then
tell me to quit
end if
repeat with i from 1 to count of listofcolors
if item i of listofcolors is thecolor then set position to i
end repeat
return (position)
end picklabelcolor
set theFolder to POSIX path of (choose folder)
display dialog "What type of file would you like to label?" default answer ""
set fileextension to text returned of result
set labelnumber to picklabelcolor()
if labelnumber is 0 then error number -128
set foundFiles to paragraphs of (do shell script "mdfind -onlyin " & quoted form of theFolder & " 'kMDItemFSName = \"*." & fileextension & "\"'")
repeat with aFile in foundFiles
set aFilePath to POSIX file aFile as text
tell application "Finder" to set label index of item aFilePath to labelnumber --or 2 or 3, etc.
end repeat
on picklabelcolor()
set listofcolors to {"1 Orange", "2 Red", "3 Yellow", "4 Blue", "5 Purple", "6 Green", "7 Gray"}
set thecolor to (choose from list listofcolors with title "Possible Colors" with prompt "Choose A Label Color")
if thecolor is false then return 0
return word 1 of item 1 of thecolor as integer
end picklabelcolor
I am still having a problem. In the line where we tell Finder to set the label, I get a file Network permission error - all the files I am labeling are on the local drive.
You may have one or more files with special permissions in the chosen folder. Try this. It works from Lion to Mavericks. It handles any files with special permissions, for instance if a file in the chosen folder happens to have system and wheel permissions.
set theFolder to POSIX path of (choose folder)
display dialog "What type of file would you like to label?" default answer ""
set fileextension to text returned of result
set labelnumber to picklabelcolor()
if labelnumber is 0 then error number -128
set foundFiles to paragraphs of (do shell script "mdfind -onlyin " & quoted form of theFolder & " 'kMDItemFSName = \"*." & fileextension & "\"'")
repeat with aFile in foundFiles
try
set aFilePath to POSIX file aFile as text
tell application "Finder" to set label index of item aFilePath to labelnumber --or 2 or 3, etc.
on error
set qaFile to quoted form of aFile
set chmodOrig to text -3 thru end of (do shell script "stat -f \"%p\" " & qaFile) --get the original permissions of the file
activate --to focus on the password dialog box.
do shell script "chmod 777 " & qaFile with administrator privileges --set permissions to "all free"
set aFilePath to POSIX file aFile as text
tell application "Finder" to set label index of item aFilePath to labelnumber --or 2 or 3, etc.
activate --to focus again on the password dialog box if required.
do shell script "chmod " & chmodOrig & " " & qaFile with administrator privileges --restore original permissions
end try
end repeat
on picklabelcolor()
set listofcolors to {"1 Orange", "2 Red", "3 Yellow", "4 Blue", "5 Purple", "6 Green", "7 Gray"}
set thecolor to (choose from list listofcolors with title "Possible Colors" with prompt "Choose A Label Color")
if thecolor is false then return 0
return word 1 of item 1 of thecolor as integer
end picklabelcolor
Unless you have a special reason, I’d also suggest to allow for the possibility to remove labels as well. For that, remove the line
if labelnumber is 0 then error number -128
and change the handler to
on picklabelcolor()
-- set listofcolors to {"1 Orange", "2 Red", "3 Yellow", "4 Blue", "5 Purple", "6 Green", "7 Gray"}
set listofcolors to {"0 none", "1 Orange", "2 Red", "3 Yellow", "4 Blue", "5 Purple", "6 Green", "7 Gray"}
set thecolor to (choose from list listofcolors with title "Possible Colors" with prompt "Choose A Label Color")
-- if thecolor is false then return 0
if thecolor is false then error number -128
return word 1 of item 1 of thecolor as integer
end picklabelcolor