Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 12 votes, 4.92 average. Display Modes
Old 04-24-2002, 09:53 AM   #1
bluehz
MVP
 
Join Date: Jan 2002
Posts: 1,562
osascript

Can any one explicitely explain or point me to some explicit information regarding the use of osascript - more precisely the use of osascript and its interaction with cron. I must have spent four hours last night perusing various posts around the net gathering information on the use of osascript with cron. Just when I think I got it down - it works once, then the next run it doesn't.

I am so confused now as to what works and what doesn't work!!!

Any help would be greatly appreciated!
bluehz is offline   Reply With Quote
Old 04-24-2002, 11:56 AM   #2
bakednotfried
Prospect
 
Join Date: Jan 2002
Location: fort collins, co
Posts: 31
is it cron or osascript?

hey bluehz,

i don't use osascript, but your symptom makes me wonder if it's a cron issue. can you get anything to run with cron using the schedule you want? take osascript out of the picture and see if you can get cron to execute a simple shell script on the schedule you want.

hope this helps,
mike
__________________
-- mike cullerton
bakednotfried is offline   Reply With Quote
Old 04-24-2002, 12:11 PM   #3
bluehz
MVP
 
Join Date: Jan 2002
Posts: 1,562
cron works perfectly for other shell scripts I have written, etc.
bluehz is offline   Reply With Quote
Old 04-24-2002, 01:09 PM   #4
bakednotfried
Prospect
 
Join Date: Jan 2002
Location: fort collins, co
Posts: 31
what's different

i've never used osascript, so i'm wondering what's different about it than running anything else from cron. is osascript a script itself? a binary?

one time in the past, i seem to remember a problem where the first instance of cron running something wasn't finished when cron tried the next instance. that caused problems. had to change crontab to run it less frequently.

good luck,
mike
__________________
-- mike cullerton
bakednotfried is offline   Reply With Quote
Old 04-25-2002, 02:59 AM   #5
honestpuck
Major Leaguer
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 256
bluehz,

Have you got osascript wrapped in a shell script or are you trying it bare?

I've had problems with cron and command line paramters and now always wrap things in a shell script.

Tony
honestpuck is offline   Reply With Quote
Old 04-25-2002, 06:56 AM   #6
bluehz
MVP
 
Join Date: Jan 2002
Posts: 1,562
I have it wrapped in a shell script - runs fine manually, runs fine through cronnix, just refuses to run via cron activity. Really irritates me that cron task have to be this hard. Shouldn't be like this.
bluehz is offline   Reply With Quote
Old 04-25-2002, 08:08 AM   #7
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
i'm seeing more of this and it makes me wonder: does your scriptage do anything that requires a head or terminal or an instance of the finder?

could you post the meat & potatoes of your scripts, the crontab command line and any meaningful errors?
mervTormel is offline   Reply With Quote
Old 04-25-2002, 11:49 AM   #8
bluehz
MVP
 
Join Date: Jan 2002
Posts: 1,562
No problem - although what I am posting is somewhere near variation 999 that I have tried to get to work in cron. Its really quite a basic script - all I want it to do is regularly update the Library in iTunes by scanning the ~/Music folder. It checks for iTunes and launches it if it is not runninng already. Should be no big deal....

This is the shell script that is 'run' by cron....saved in a text file add.sh, proper permissions granted.

Code:
 #!/bin/sh
osascript <<EOF
using terms from application "Finder"
tell application "Finder"
	if (get name of every process) contains "iTunes" then
		--do nothing
	else
		do shell script "open /Applications/iTunes.app"
		delay 15
	end if
end tell
end using terms from
using terms from application "iTunes.app" --of machine "eppc://127.0.0.1"
	tell application "iTunes.app" --of machine "eppc://127.0.0.1"  to 
		ignoring application responses
			add {"volnam:path:to:Music:"} to playlist "Library"
		end ignoring
	end tell
end using terms from
EOF
I have tried the crontab as both the user and also the system crontab - neither works, but it seems like the system crontab works best/closest. This is the cron entry (system crontab)

23 11 * * * root sh /path/to/add.sh

yields this error (reported via cron mail):
syntax error: Can't get the application's event dictionary. (-2709)

----

I have also tried saving the script as a compiled applescript....add.scpt, proper permissions, etc.
Code:
 
using terms from application "Finder"
tell application "Finder"
	if (get name of every process) contains "iTunes" then
		--do nothing
	else
		do shell script "open /Applications/iTunes.app"
		delay 15
	end if
end tell
end using terms from
using terms from application "iTunes.app" --of machine "eppc://127.0.0.1"
	tell application "iTunes.app" --of machine "eppc://127.0.0.1"  to 
		ignoring application responses
			add {"volnam:path:to:Music:"} to playlist "Library"
		end ignoring
	end tell
end using terms from
and launching from cron:

23 11 * * * root /usr/bin/osascript /path/to/add.scpt

which yields this error:
execution error: Finder got an error: Application isn't running. (-600)

None of these will work (and many other variations I tried).

Obviously - I have edited them abit to remove personal data - /path/to, etc...

Last edited by bluehz; 04-25-2002 at 11:56 AM.
bluehz is offline   Reply With Quote
Old 04-25-2002, 01:02 PM   #9
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
this is what i was afraid of. the dreaded -finder got an error: app isn't running-

i stripped that osascript call down to tell finder do shell script open itunes and it gakked.

there is something missing in a headless cron sh environment that a call to osascript gaks on. the -600 error

i can find nothing about how to resolve this...

i suggest you make a very simple sh script to /usr/bin/osascript call, get the
-600 error and report it to apple.
mervTormel is offline   Reply With Quote
Old 04-25-2002, 04:22 PM   #10
bluehz
MVP
 
Join Date: Jan 2002
Posts: 1,562
You wouldn't happen to know what the 600 error is would you?

Thx for looking at it...
bluehz is offline   Reply With Quote
Old 04-25-2002, 08:30 PM   #11
honestpuck
Major Leaguer
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 256
put the open in the shell script

Well if your Applescript is gakking on the opening of iTunes why don't you do that in your shell script before you run osascript.

Something like

# we subtract 2 - 1 for iTunes Helper and one for our grep process
if ! ( `ps ax | grep iTunes -c` - 2 ) open /Applications/iTunes.app

would do it for you if you make sure it runs in tcsh.

Tony
honestpuck is offline   Reply With Quote
Old 04-25-2002, 08:34 PM   #12
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
Quote:
You wouldn't happen to know what the 600 error is...

the only reference i could find is in this [ at least three years old ] documentation...

http://developer.apple.com/techpubs/...cript.10a.html

upon reflection, i really think this is the call to /usr/bin/osascript

try making the simplest thing to do in an osascript call and i'll bet it gaks from cron because osascript needs an environment that a headless cron sh process doesn't have.

the trick is to provide a more robust environment; i just don't know enough about AS to get you there.

would someone test my ASSumption above?
mervTormel is offline   Reply With Quote
Old 04-25-2002, 10:56 PM   #13
bluehz
MVP
 
Join Date: Jan 2002
Posts: 1,562
Thanks - thats my problem also - all the AS documentation is outdated. Very little info on OSASCRIPT if any.
bluehz is offline   Reply With Quote
Old 04-26-2002, 10:01 AM   #14
honestpuck
Major Leaguer
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 256
A Workaround!!!

OK, after much experimentation I have a workaround for this problem.

If you have a shell script '/Users/myname/bin/shellscript' that contains an osascript command and you want to run it using cron.

Step 1.

Create a single line AppleScript that reads :-
do shell script "/Users/myname/bin/shellscript"

and save that shell script as an application called (for example) runshellscript.

Step 2

In your crontab you need to have a command :-

open ~/bin/runshellscript

and all will work.

Tony
honestpuck is offline   Reply With Quote
Old 04-26-2002, 11:30 AM   #15
bluehz
MVP
 
Join Date: Jan 2002
Posts: 1,562
Ok - that sounds good. If I understand this then - you are basically launching an AS app front-end to launch the shell script that launches the final AS through the OSASCRIPT cmd. Why would I want to do this instead of just including ALL the AS in the FINAL AS (that is run via the OSASCRIPT) into the first AS application? Understand?

My whole point was to really avoid the AS application of possible. In my experience - the whole process of launching the app either interupts your work flow, pauses the machine, changes the frontmost app, etc....only occurs when the AS is run as a standalone-app...thats why I was trying to run it via the compiled script instead.

I guess there are probably means to create the initial front-end AS app to be less intrusive. I am no AS guru - so I am not really familiar - maybe "igoring..."

Thx for the info though - I think we are getting somewhere....theoretcically we could create the frontend AS app to accept variables and it could be a frontend to all the AS you want to run from cron/shell, etc.
bluehz is offline   Reply With Quote
Old 04-26-2002, 11:55 AM   #16
porkchop_d_clown
Triple-A Player
 
Join Date: Jan 2002
Location: Phoenixville, PA
Posts: 127
Question I believe that the problem is in CRON

cron has an oddity that catches lots of UNIX programmers - it doesn't load the user's environment when it executes. Things like the PATH, environment variables, etc... don't get set.

I'm trying to work this out now, I'll let you know if I figger anything out!
__________________
Knock Knock.
porkchop_d_clown is offline   Reply With Quote
Old 04-26-2002, 12:02 PM   #17
honestpuck
Major Leaguer
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 256
Quote:
Originally posted by bluehz
Ok - that sounds good. If I understand this then - you are basically launching an AS app front-end to launch the shell script that launches the final AS through the OSASCRIPT cmd. Why would I want to do this instead of just including ALL the AS in the FINAL AS (that is run via the OSASCRIPT) into the first AS application? Understand?

Oh, sure, I understand. But in some instances it's much easier to do a bunch of stuff in Perl or the shell than it is in AppleScript but you need that little bit of AS. Much more powerful languages. The reason I solved it was for someone who wanted to load a particular desktop picture based on the time; hard work in badly documented AppleScript, dead easy in well documented Perl.

OK, it's not ideal, but it does work - and while it does bring up the AS as an app it quits right back to exactly the same app you had running in a matter of a second or so - before the shell script it calls completes.

It also proves that the problem isn't cron but osascript. It appears osascript doesn't like being called from a headless app. I'm going to try for a better solution, but this one works in the meantime.

Tony
honestpuck is offline   Reply With Quote
Old 04-26-2002, 12:38 PM   #18
porkchop_d_clown
Triple-A Player
 
Join Date: Jan 2002
Location: Phoenixville, PA
Posts: 127
What the heck is a headless app?

If you mean "A background app" then why does

Quote:
osascript random_background </dev/null >/dev/null &

work correctly? Things don't get more "headless" than that.
__________________
Knock Knock.
porkchop_d_clown is offline   Reply With Quote
Old 04-26-2002, 12:40 PM   #19
honestpuck
Major Leaguer
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 256
Re: I believe that the problem is in CRON

Quote:
Originally posted by porkchop_d_clown
cron has an oddity that catches lots of UNIX programmers - it doesn't load the user's environment when it executes. Things like the PATH, environment variables, etc... don't get set.

I'm trying to work this out now, I'll let you know if I figger anything out!

While the users environment isn't set the defaults are, so you actually get quite a lot of the environment variables and the best bits of PATH (like /bin /usr/bin).

In the case of osascript it is actually the headlessness that seems to catch it. Build yourself any headless app and you will discover that it has all the same drawbacks as cron visavi the environment you get exposed. An easy way is to use Apache to run a Perl script and guess what, you have osascript gakking in exactly the same way.

Tony
honestpuck is offline   Reply With Quote
Old 04-26-2002, 12:45 PM   #20
porkchop_d_clown
Triple-A Player
 
Join Date: Jan 2002
Location: Phoenixville, PA
Posts: 127
You're still going to have to define "headless"

What is a "headless" application? I know about background apps, foreground apps, daemons, nohup and kernelspace, but I've never run into a "headless" application.
__________________
Knock Knock.
porkchop_d_clown is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 06:24 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.