Saturday, October 25, 2014

#1 2008-12-09 06:39:12 pm

phaworth
Member
Registered: 2007-02-23
Posts: 21

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:

Applescript:


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


Filed under: Finder

Offline

 

#2 2008-12-09 07:46:36 pm

cwtnospam
Member
Registered: 2008-06-08
Posts: 274

Re: osascript command line parameter

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:

Applescript:

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


Filed under: Finder

Offline

 

#3 2008-12-09 09:50:06 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2649

Re: osascript command line parameter

phaworth wrote:

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

From the osascript man page:

Any arguments following the script will be passed as a list of strings to the direct parameter of the ``run'' handler.


Filed under: man

Offline

 

#4 2008-12-10 12:04:48 am

phaworth
Member
Registered: 2007-02-23
Posts: 21

Re: osascript command line parameter

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

Offline

 

#5 2008-12-10 06:51:22 am

cwtnospam
Member
Registered: 2008-06-08
Posts: 274

Re: osascript command line parameter

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/

Offline

 

#6 2008-12-11 07:16:06 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 3550

Re: osascript command line parameter

phaworth wrote:

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

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.

Applescript:

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:

Applescript:

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':

Applescript:

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'


NG

Filed under: Finder

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.034 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)