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



Reply
 
Thread Tools Rating: Thread Rating: 47 votes, 5.00 average. Display Modes
Old 01-17-2004, 11:10 PM   #1
detlef
Triple-A Player
 
Join Date: Dec 2003
Location: Tokyo, Japan
Posts: 76
Question how to use multiple -e command in osascript ?

Hi,

in the osascript man pages it says that multiple -e command can be put together in one command line.

-e command

Enter one line of a script. If -e is given, osascript will not look for a filename in the argument list. Multiple -ecommands may be given to build up a multi-line script. Because most scripts use characters that are special to many shell programs (e.g., AppleScript uses single and double quote marks, ``('', ``)'', and ``*''), the command will have to be correctly quoted and escaped to get it past the shell intact.

I assume that the multiple -e command can be used to execute a small applescript in one command line without having calling an applescript with osascript.

The question I have is: How does the code for a multiple -e command look like?

As an example I would like to execute the following applescript at the command line:

tell application "Finder"
delete file "Untitled.rtf"
end tell

Detlef
detlef is offline   Reply With Quote
Old 01-18-2004, 12:22 AM   #2
robJ
Major Leaguer
 
Join Date: Aug 2003
Posts: 429
You example is actually one command which could be expressed as:
Code:
tell application "Finder" to delete file "Untitled.rtf"
Barring typos, the shell command is:
Code:
osascript -e 'tell application "Finder" to delete file "untitled.rtf"'
An example of multiple commands would look something like:

Code:
tell application "Finder"
	delete file "Untitled.rtf"
	delete file "untitled2.rtf"
end tell
The shell command would look like this:
Code:
osascript -e 'tell application "Finder" to delete file "untitled.rtf"' -e 'tell application "Finder" to delete file "untitled2.rtf"'
Note: In these examples, which lack full paths to the files, the Finder will look for the files on the desktop.

-- Rob
robJ is offline   Reply With Quote
Old 01-18-2004, 02:02 AM   #3
detlef
Triple-A Player
 
Join Date: Dec 2003
Location: Tokyo, Japan
Posts: 76
Lightbulb

good explanation. Thank you very much.

Detlef
detlef is offline   Reply With Quote
Old 01-18-2004, 08:13 PM   #4
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
The "multiple -e" method of using longer applescripts works OK, but it's also worth knowing the "HEREDOC" method, which allows you to cut and paste applescript snippets into shell scripts and have them executed by osascript. An example should do the trick... here's a command that makes a new terminal window offset from the frontmost such thing. (10.3's terminal now behaves perfectly, so the script's pretty much useless: but it illustrates the technique). I called it "nt", and threw it in my ~/bin directory. You call it using something like "nt 'cd junk'", or just a raw "nt".

Code:
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
The "END" can be any string: just make sure that there's not trailing space after either occurrence.

Cheers,
Paul
pmccann is offline   Reply With Quote
Old 01-22-2004, 10:11 AM   #5
detlef
Triple-A Player
 
Join Date: Dec 2003
Location: Tokyo, Japan
Posts: 76
pmccann,

thanks for the hint.
I finally got a chance to try it out with some of my applescripts. It works great.

Detlef
detlef 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 03:43 AM.


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.