osascript command line parameter

I’ve seen various posts on this topic in the forums but can’t quite get this to work. Here’s the scenario.

I want to run an Applescript and pass in a parameter which is a path to a preferences file to be used by the script. In Terminal, I type in 'osascript ', drag the Applescript file from a Finder window into Terminal, type a space, then drag the preferences file from Finder into Terminal. I have a display dialog command in the AppleScript and I see that the parameter is being passed in correctly but my script throws an error that it can’t find the preferences file.

I did see that the filename in Terminal had “/” chars as a separator rather than “:” so I tried the osascript command again usning “:” as the delimiter but same result.

Before trying this, the script used to prompt fro the file name with a choose file command and open the file using the result and that works fine using the same file as I’m trying to open with the osascript command.

The Applescript that gets the argument and tries to open the file is:


on run argv
	tell application "Finder"
		set thePrefsFile to item 1 of argv
                display dialog thePrefsFile
		if exists thePrefsFile then
 			set FileRef to open for access thePrefsFile
                        ....
               else
			display dialog "Cannot find Preferences File. Run Prefs Maintenance script to create it" with icon stop
               end if

 

Do I need to coerce the osascipt parameter into some other type?

Once I’ve got this figured out, I want to store the osascript command in a file and then execute it from either cron or as a recurring iCal event. Will that file need a special extension in order for it to be recognised?

Thanks,
Pete

The only way I know to do that is to have the shell script call an Applescript file:

#!/bin/bash
/usr/bin/osascript /path/to/myapplescript.scpt ‘easily passed to an Applescript file.’

myapplescript.scpt:

on run argv
	tell application "Finder"
		activate
		display dialog "The argument is " & item 1 of argv
	end tell
end run

From the osascript man page:

Been playing around with this a bit more and found that if all I pass in as the parameter is te actual filename without any path info, the script works just fine. That worls for me since the files in question will always be in the folder.

So the only remaining question is the command file to run the osascrip command from iCal or cron… I now know what the commnd should look like - when I stroe that in a file does it have to have a special extension of some sort in order for it to be recognised as a script or terminal command file of some sort?

Thanks,
Pete

I’d use Lingon (cron is deprecated) to set launchd to run the shell script, or I’d use iCal to run an Applescript from an alarm.

http://tuppis.com/lingon/

It depends what the parameter is that you’re passing! If you’re passing a POSIX path, which is what you get when you drag your preference file into Terminal, your script will need to coerce it to a ‘POSIX file’ before ‘open for access’ or the Finder can use it.

on run argv
	set thePrefsFile to (item 1 of argv) as POSIX file -- We're expecting a POSIX path.
	tell application "Finder" to set fileExists to (thePrefsFile exists)
	if (fileExists) then
		set FileRef to (open for access thePrefsFile)
		 --- ...
	else
		display dialog "Cannot find Preferences File. Run Prefs Maintenance script to create it" with icon stop
	end if
end run

Better still, to dispense with the Finder altogether:

on run argv
	set thePrefsFile to (item 1 of argv) as POSIX file -- We're expecting a POSIX path.
	try
		set thePrefsFile to thePrefsFile as alias -- This will error if the file doesn't exist.
		set FileRef to (open for access thePrefsFile)
		 --- ...
	on error
		display dialog "Cannot find Preferences File. Run Prefs Maintenance script to create it" with icon stop
	end try
end run

If you’re passing an HFS path, you’ll need to pass the entire path, starting with the disk name ” if you know what that is. Even so, it’s better for the script to convert it to a file or an alias for ‘open for access’:

on run argv
	set thePrefsFile to item 1 of argv -- Here we're expecting an HFS path.
	try
		set thePrefsFile to thePrefsFile as alias -- This will error if the file doesn't exist.
		set FileRef to (open for access thePrefsFile)
		 --- ...
	on error
		display dialog "Cannot find Preferences File. Run Prefs Maintenance script to create it" with icon stop
	end try
end run

In either case, it’s best to quote the paths to both the script and the preferences file, in case they have spaces in them:

osascript ‘/path/to/script’ ‘/path/to/preference file’