« Religious Writes | Main | Love »
March 02, 2006
More scripting with oascript
Hi Ozguru, I read your January blogs using osascript. I am trying to label some directories according to criteria in a Bash script. Basically, the script counts items in a folder and if there are less than a certain number, I want it to be colored gray. After reading a bit, it's my understanding the only way to do Finder labeling from Unix is to use the osascript call. I also found Codepoet's MacGeekery post you mentioned, and the following command works fine for files:
find -E . -iregex '.*\.jpg$' -exec osascript \
-e "tell application \"Finder\" to set the label index of \
POSIX file \"$PWD/{}\" to 7" \;
But for directories, no joy. I then found your post from last month, but can't seem to get it to work.
What you came up with:
tell application \"Finder\"
set fileName to posix file \"some file or directory\" as file
set the label index of item fileName to 7
end tell
As most geeks would know, you can experiment with Apple scripts in the Script Editor (Applications / Utilities / Script Editor) but there are a couple of tricky bits - the first is that it is impossible to use the posix path stuff in the apple script editor because it always compiles the expression and therefore the behaviour does not match. The second tricky part is to make sure you use "item" instead of "directory" or "folder" or "file" (which you did). So, now we need to put it together with something like this (I created a new folder called "Foo" in the current directory).
#!/bin/bash
osascript <<!!
tell application "Finder"
set fileName to posix file "$PWD/Foo" as file
set label index of item fileNmae to 3
end tell
!!
That combines setting a directory attribute with the idea of a heredoc. In theory the multiple arguments method of talking to oascript should work but I always have problems with it. Give that a whirl and let me know how it goes...
Posted by Ozguru at March 2, 2006 06:00 AM