• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Running scripts from cron AND passing arguments. A solution.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Running scripts from cron AND passing arguments. A solution.


  • Subject: Running scripts from cron AND passing arguments. A solution.
  • From: Lee Noble <email@hidden>
  • Date: Fri, 16 Sep 2005 18:03:56 +0100

I appreciate this might be old news to some but I can't find any mention of it in my backlog of 9300 messages received from this list in the last year, but I'm so pleased at finally finding a general solution to two problems at once that I thought I would share.

Problem 1
You can't pass arguments to osascript for the target script to act on. I know that in 10.4 this is solved but I'm still running 10.3 so forgive me.


Problem 2
Any script which you launch via the cron using osascript whether embedded in a shell script, perl script or whatever will fail if it requires the GUI, which in my experience has been most things. I've run into this wall a lot, so many times I've developed a script which works perfectly in script editor, then works from the cron and then fails when I am logged in but switched out meaning I have no choice but to stay logged in and disable the screensaver or use iCal to launch the script, but then iCal can't pass arguments either (see problem 1) and I can find no reliable way to fetch variables from a specific iCal event. If only iCal had a command which just sent the unique ID of an event to a script running on its alarm.


The Solution
Save this as a shell script - I've called mine osaargs. Make sure it's chmoded + x and stick it in your path.


#!/bin/sh
# take the first argument as the path to the folder with the attached folder action
thePath="$1"


# alternatively use this line (adjust accordingly and uncomment) which hardcodes an Action Folder
# into the script. I thought an appropriate place was in with the Folder Actions
# themselves. Call it "Action Folders".
#thePath="/Users/yourusername/Library/Scripts/Folder Action Scripts/Action Folders/$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"

# --------------- END OF SCRIPT ------------------#

You then need this AppleScript

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
--	"				"
--	"				"	

	tell application "Finder"
		activate
		move file (actionFile as string) to the trash
	end tell

	(*

		PLACE YOUR SCRIPT HERE

	*)
end adding folder items to
-- END OF SCRIPT --

You then need to attach your script to a folder. You may follow my example and create a
folder inside ~/Library/Scripts/Folder Action Scripts called Action Folders. Into this folder
I place a new folder for each script and name it the same as the script.


Then you simply call the shell script like so. The first argument is the path to the
folder with the attached action. subsequent arguments appear as var# in the applescript.


osaargs "/path/to/your/folder" banana "argument 2" tree

or if you followed my folder setup

osaargs "folder name" banana "argument 2" tree

Since folder action scripts are not constrained by GUI limitations like cron scripts are
you can do what you like. Stay logged in and switched out and the scripts still work.


If this is helpful to one person then I have achieved my aim.

Lee

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Running scripts from cron AND passing arguments. A solution.
      • From: "Mark J. Reed" <email@hidden>
  • Prev by Date: Re: How to deal with Asynchronous Finder operations
  • Next by Date: Re: """
  • Previous by thread: Re: """
  • Next by thread: Re: Running scripts from cron AND passing arguments. A solution.
  • Index(es):
    • Date
    • Thread