Sep 19, '05 09:52:00AM • Contributed by: leenoble_uk
Unfortunately the script fails because it doesn't work with recurring events. If only iCal could pass the unique event identifier to alarm scripts, it would solve everything. But it doesn't. So I looked at using crontab to run my script contained within a shell script. But I ran into a wall I've run into many times before: You can't run AppleScripts which require user interaction from cron unless you are logged in. My scripts also make use of UI Scripting, which won't work behind a screensaver. The only option is to be logged in and switched out and use iCal.
The above hint launched me along the path to finally discovering the solution: Folder Actions. A simple shell script could be passed arguments from cron and then drop them into a text file in a folder. A Folder Action would then act on that file and extract variables from it.
Here's the shell script:
#!/bin/sh
# take the first argument as the path to the folder
# with the attached folder action
thePath="$1"
# remove existing arguments file if it exists.
# The -f just supresses the error if the file does not exist
rm -f "${thePath}/folder-action-arguments.txt"
# this tiny delay is necessary because if the file
# did exist before then Finder will not notice the new file
# arriving as it is replaced too quickly otherwise
sleep 4
# this bumps the first argument off the list (defined path)
shift 1
# the curly braces are used to supress output until the code
# block is complete, then everything generated in the block is
# output to file at the end (after the closing brace)
{
for i in "$@"
do
echo ${i}
done
}>"${thePath}/folder-action-arguments.txt"
I've saved the script as osaargs in /usr/bin. It takes the first argument as the path to the folder with the attached action you want to perform:
osaargs "/Users/your_user/foldername" "argument one" banana "argument 3" fish
It will then create a new text file in the specified folder, which this AppleScript will pick up and extract the arguments from:
on adding folder items to actionFolder after receiving actionFile
set thePath to quoted form of (POSIX path of actionFile)
set scriptToDo to "cat " & thePath
set argArray to (do shell script scriptToDo)
set passedArgs to paragraphs of argArray
set var1 to (item 1 of passedArgs)
set var2 to (item 2 of passedArgs)
set var3 to (item 3 of passedArgs)
(*
DO YOUR STUFF HERE USING THE var# VARIABLES
*)
tell application "Finder"
move file (actionFile) to the trash
end tell
end adding folder items to
The script only moves the file to the trash; it doesn't empty it just in case you manually drop something into that folder at some stage. Since the folder action was launched independently of the cron, it bypasses the security restrictions placed on cron scripts and can perform UI interaction, too.
[robg adds: I haven't tested this one.]
