Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Run AppleScripts from the Terminal with arguments UNIX

I didn't find this anywhere, so I cooked it up myself. The gist is that you can pre-compile your AppleScripts and pass arguments to them via Terminal -- no need to dynamically generate and compile the scripts via the osascript command.

Read the rest of the hint for the how-to...

[robg adds: I haven't tested this one, and it requires installing a Perl module, so there's a bit of assumed UNIX knowledge...]

Here is how it is done:
  1. Make sure you have the Mac::AppleScript Perl module installed. Do this with the cpan command or how ever you prefer.
  2. Create a text file (osarun) with the following contents:
    #!/usr/bin/perl
    
    use strict;
    use Mac::AppleScript qw/ RunAppleScript /;
    
    if( $#ARGV = 0) ? '"'.join('","',@ARGV).'"' : "";
    
    my $rtn = RunAppleScript( "return run script alias ((POSIX file ""
          .$script.'") as text) with parameters {'.$args.'}' ),"n"
            or die "AppleScript Error: $!";
    
    $rtn =~ s/(^"|"$)//g;
    
    print $rtn,"n";

    Feel free to make this more user friendly.... Save this file on your PATH and make it executable.

  3. Open Script Editor and create your script. Here is an example:
    on run argv
            set rtn to ((count of argv) as text) & " parameters passed.
    The parameters were:
    "
      repeat with arg in argv
                   set rtn to rtn & "      " & (arg as text) & "
    "
          end repeat
        
          return rtn
    end run
    

    The variable argv will be a list of the command line arguments; do anything you want with them. Be sure to save your script as a script or application (just not as text). You can also use osacompile to compile scripts.

  4. Now you can run the AppleScript from the command line:
    osarun Callee.scpt test "second test"

    This should give you the output:

    2 parameters passed.
    The parameters were:
            test
            second test
              

That's it. Now go crazy. Write AppleScripts.

    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[10,877 views]  

Run AppleScripts from the Terminal with arguments | 8 comments | Create New Account
Click here to return to the 'Run AppleScripts from the Terminal with arguments' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
The long way?
Authored by: gatorparrots on Feb 05, '04 11:43:00AM
This seems like the long way around the problem. We've discussed a different solution in the forum already. The solution basically involves using a heredoc in the shell, so arguments can be fed in as well. Here is Paul McCann's example:
osascript<<END 
set init to {40, 40} --top left corner of window relative to top left of screen 
set dx to 20 --amount to shift each new window right 
set dy to 30 --amount to shift each new window down 
tell application "Terminal" 
set thewindows to (every window) 
set num to the length of thewindows 
do script "$1" 
set tl to {(item 1 of init)+dx*num,(item 2 of init)+dy*num} 
set position of window 1 to tl 
activate 
end tell 
END


[ Reply to This | # ]
The long way?
Authored by: pmccann on Feb 05, '04 06:50:47PM

Here's a much shorter example which might highlight what's going on a little better:

osascript<<END
tell app "Finder" to open ("$1" as POSIX file as alias)
END

Save this as "pmo" (for "poor man's open") or whatever in your ~/bin directory --assuming that /Users/yourusername/bin is in your PATH-- make it executable (chmod u+x ~/bin/pmo), enter "rehash" to get it found and you should be able to do something like

pmo filename.txt

or

pmo ~/bin/pmo

and so forth. (The Finder should use the correct app to open the file.) As promised by the name, it's a pretend "open" command.



[ Reply to This | # ]
The (really!) long way?
Authored by: Krioni on Feb 06, '04 09:45:34AM
Wow. Or you could just use the actual 'open' command. Type

man open
in the Terminal. It opens a file the same way the Finder would, but also lets you 'open' URLs, specify an app, or whatever. No need for "poor man's open" I think.

[ Reply to This | # ]
The (really!) long way?
Authored by: pmccann on Feb 09, '04 06:54:31PM

Oh really? To think that I hadn't noticed. Thanks so much for open-ing my eyes. Now I can go off and recreate some other wheels.

Sheesh...



[ Reply to This | # ]
The long way?
Authored by: jaysoffian on Feb 07, '04 04:12:29PM

The problem with using a HERE document is that osascript has to compile the entire applescript each time. That may be a length operation if you are either running the script multiple times, or it is a long script. Also, you have to emed the entire applescript inside your sh script, which you may not wish to do.

The advantage of this hint, over a here document, is that you can run a pre-compiled script and pass it arguments.



[ Reply to This | # ]
The long way?
Authored by: formido on Feb 10, '04 02:25:20PM

A good point, but if that really becomes a problem, you can just put the main script in a separate, pre-compiled file and only call it from the HERE doc. This allows the complexity of set-up to scale with your needs.



[ Reply to This | # ]
Run AppleScripts from the Terminal with arguments
Authored by: macman13 on Feb 05, '04 05:02:16PM

The coolest thing for me about this is that I never new you could compile a plain text script from the terminal before. I can now write scripts in the terminal as plain text files and compile and run them from there. Not only that, but I could write javascript scripts as plain text and compile them using osacompile and designate the javascript language. Interesting stuff. Is there more documentation on running applescript from the terminal?

---
\\\"I can do everything on my Mac I used to do on my PC, plus alot more ...\\\"
--Me



[ Reply to This | # ]
osasubr
Authored by: karenwallace on Feb 06, '04 08:51:11AM
There's a command line utility for this called osasubr if you wish to muck less.

[ Reply to This | # ]