Wednesday, February 29, 2012

Nifty Box - Add Finder Color Label to Untagged Files in Particular Folder AppleScript

This script will let you pick a color and a folder to scan. It will not color folders, just files. Its helpful in finding unneeded files. You can add a tag to important files and remove those files which are not needed.

--color selection
set theColors to {"blank", "orange", "red", "yellow", "blue", "purple", "green", "gray"}

set myColor to (choose from list theColors)
if (myColor is false) then error number -128
set myColor to item 1 of myColor

--this part finds out what theIndex (number) the label is.
set a to 1
repeat until ((item a of theColors) is myColor)
set a to (a + 1)
end repeat
set theIndex to a - 1

-- Or, in this case, simply:
-- tell application "Finder" to set label index of entire contents of mainFolder to theIndex

-----------------------end of color selection code
--i get the desired nifty box tagged items--ouput is posix path names in list "taggedList"
tell application "Nifty Box"
set tagged_items to nbitems
set taggedPosix to {}
repeat with x in tagged_items
set end of taggedPosix to item location of x
end repeat
end tell
tell application "Nifty Box"
set f to folder "test" -- top level folder
set taggedList to tagged nbitems of f
set taggedPosix to {}
repeat with i in taggedList
set end of taggedPosix to item location of i
end repeat
end tell
--i get files in folder which should be checked for tags--outout is posix path names--and put all files in list "filesList"
set myDir to (choose folder)

-- we have to remove the trailing / from the folder path so the
-- returned paths from the find command are correct
set posix_folder to text 1 thru -2 of (POSIX path of myDir) -- text 1 thru -2 of (POSIX path of myDir) to remove trailing forward slash

-- use find to quickly find the items in this_folder
-- also filter out invisible files and files in invisible folders
-- also filter files inside of package files like ".app" files
set allfilesPosix to paragraphs of (do shell script "find " & quoted form of posix_folder & " -name \"*.*\" ! -path \"*/.*\" ! -path \"*.app/*\" ! -path \"*.scptd/*\" ! -path \"*.rtfd/*\" ! -path \"*.xcodeproj/*\"") as list
--set allfilesPosix to allfilesPara as list
--i do the color labels

--this line marks the end of the POSIX code
--i convert both lists, taggedPosix->taggedList and allfilesPosix->allfilesList to AppleScript References
set taggedList to {}
repeat with i in taggedPosix
set end of taggedList to POSIX file i
end repeat

set allfilesList to {}
repeat with i in allfilesPosix
set end of allfilesList to POSIX file i
end repeat

set untaggedPosix to {}
repeat with theItem in allfilesPosix
if theItem is not in taggedPosix then set end of untaggedPosix to (contents of theItem)
end repeat
get untaggedPosix

set untaggedList to {}
repeat with i in untaggedPosix
set end of untaggedList to POSIX file i
end repeat
get untaggedList

tell application "Finder"
repeat with i in untaggedList
set label index of (item i) to theIndex
end repeat
end tell

No comments:

Post a Comment