• 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: Problem with osascript command
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with osascript command


  • Subject: Re: Problem with osascript command
  • From: Christopher Nebel <email@hidden>
  • Date: Wed, 28 Nov 2007 13:22:58 -0800

On Nov 26, 2007, at 3:20 PM, Mike Kobb wrote:

I'm developing a little script that needs to be executed via osascript. It's getting run as part of an installer, and its job is to quit System Preferences if it's open.

I've written the script in Script Editor and tested it, and it works fine. However, when I invoke the same script with osascript, I'm getting incorrect behavior. I'm hoping somebody can help me. This is on Leopard, by the way (10.5.1).

Here's a scaled-down test case of my little script:

-- We refer to applications by id rather than name in case of localization
tell application id "com.apple.systemevents"
set everyProcess to bundle identifier of every process

-- Start by quitting System Preferences, since we're installing a new pref pane
if "com.apple.systempreferences" is in everyProcess then

-- activate first, so that if quitting the app presents a dialog, it will be visible
tell application id "com.apple.systempreferences" to activate
end if
end tell



As I noted, this works fine in Script Editor. If System Preferences is running, it comes to the front. If it's not running, nothing happens.


If I save this as a text file, then run it via osascript:

osascript myScript.txt

...then System Preferences launches to handle the "activate" message.

The difficulty is that 'tell application id "com.apple.systempreferences"' line. You're passing the script to osascript as plain text, which means osascript needs to compile it. When it encounters a "tell", it gets the scripting interface for that application, and for System Preferences, that (currently) requires launching it. There are two ways around this:


1. Compile the script beforehand, and run the compiled version.

2. Change 'tell application id "com.apple.systempreferences" to activate' to 'activate application id "com.apple.systempreferences"'. That doesn't invoke the get-the-scripting-interface logic.

Since you're using Leopard (I can tell since you're using "application id ..."), you don't need to invoke System Events to find out whether or not an application is running; you can just use the new "running" property instead:

if application id "com.apple.systempreferences" is running -- same as "if running of application ... is true"
activate application id "com.apple.systempreferences"
end if



--Chris Nebel AppleScript Engineering

_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
  • Follow-Ups:
    • Re: Problem with osascript command
      • From: Mike Kobb <email@hidden>
References: 
 >Problem with osascript command (From: Mike Kobb <email@hidden>)

  • Prev by Date: Re: Script Editor rewrites my script, doesn't like its rewrite
  • Next by Date: Re: Problem with osascript command
  • Previous by thread: Re: Problem with osascript command
  • Next by thread: Re: Problem with osascript command
  • Index(es):
    • Date
    • Thread