• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: osascript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: osascript


  • Subject: Re: osascript
  • From: Christopher Nebel <email@hidden>
  • Date: Thu, 25 Jul 2002 20:06:45 -0700

On Thursday, July 25, 2002, at 12:17 PM, Simon Forster wrote:

Can anyone tell me why the shell script snippet:

#!/bin/sh
osascript -e 'tell application "'$1'" to activate'

fails when passed a two word argument from the command line but works fine with a one word argument. I've tried echoing it back to the terminal and it all looks absolutely fine such as:

osascript -e tell application "TextEdit" to activate

and

osascript -e tell application "Address Book" to activate

are echoed back. If I try too run it, the first version works fine, the second doesn't - it fails with "syntax error: Expected string but found end of script. (-2741)".

Ah, the wonders of shell quoting. "echo" is a surprisingly bad way to test that you've got it right, because it just puts a space between each argument, and if the problem is that you've got two arguments where you wanted one with a space in it, you can't tell the difference.

The problem is that the $1 occurs outside of any quotes, so if it's got a space in it, you wind up with these arguments:

0: osascript
1: -e
2: tell application "Address
3: Book" to activate

...so the source passed to AppleScript is just 'tell application "Address', which of course doesn't compile. (osascript should warn you about this sort of thing, but the logic is a bit off.)

To fix it, you need to quote the argument. There are a couple of ways to go about it; either of these works:

osascript -e 'tell application "'"$1"'" to activate'
osascript -e "tell application \"$1\" to activate"


--Chris Nebel
AppleScript Engineering
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

References: 
 >osascript (From: Simon Forster <email@hidden>)

  • Prev by Date: Re: Sorting finder windows by name, date etc.
  • Next by Date: Re: Saving "traceroute" results into a variable
  • Previous by thread: Re: osascript
  • Next by thread: Two line Do Shell Script
  • Index(es):
    • Date
    • Thread