Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 09-10-2004, 01:14 PM   #1
jascha
Prospect
 
Join Date: Sep 2004
Posts: 9
AppleScript with System Preferences...?

i just started looking at AppleScript with an eye towards making some simple System Preferences scripts, and have immediately run in to a snag. the following will work with any application i have except System Preferences:

tell application "app name"
launch
end tell

When i try the above with System Preferences i get a notice saying "Syntax Error: application isn't running".

help?
jascha is offline   Reply With Quote
Old 09-10-2004, 01:41 PM   #2
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
LESSON 1: The Correct Use of The Activate Command And Launch Command (to be read in a John Cleese TV announcer voice)

"Activate" should be used to launch applications with a GUI, or bring them to the front of the screen if they have already been launched. Examples are all the well known ones i.e iPhoto and including System Preferences.app

"Launch" should be used to launch applications that do not have a GUI. i.e. Image Events.app

You can often use them interchangeably but I understand that problems can occur if you don't get it right. I guess that's what's happened in this case.
bramley is offline   Reply With Quote
Old 09-11-2004, 12:15 AM   #3
jascha
Prospect
 
Join Date: Sep 2004
Posts: 9
ah, thank you. can you give me a couple more tips? i want to write 2 scripts which will enable me to toggle between the Highest and Automatic settings in the Processor Performance menu of the Energy Saver. here's what i've got -the things in parenthesis are commands i don't know the proper syntax for.

tell application "System Preferences"
activate
end tell

tell application "System Events"
tell application process "System Preferences"
set frontmost to true
click menu item "Energy Saver" of menu "View" of menu bar 1
tell window "Energy Saver"
(click the "Options" tab)
(choose "highest" from the processor performance menu)
end tell
end tell
end tell
end tell
__________________
using: PowerBook 12", 1.33 GHz, 768 MB RAM, OS 10.3.5
jascha is offline   Reply With Quote
Old 09-11-2004, 05:44 AM   #4
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
GUI scripting (which is what the script you have posted is) isn't really Applescript - it works (half the time), but it's primary purpose is allow complex Applescripts written in XCode to control the user interfaces of other applications.

For running simple scripts i.e. written in Script Editor, GUI scripting doesn't work well. It's buggy, scripts frequently break (there is about a 50/50 chance that when 10.3.6 arrives a script working on System Preferences will stop working) and often you simply cannot do what you want to do.

The general rule is that if you find yourself writing GUI scripts in Script Editor (and in IMO Xcode) then it's because you haven't yet found a better way of doing it .....

.... and that rule applies with your script. There is currently a thread on Apple Discussions that covers this very query, which bypasses System Preferences altogether here.
bramley is offline   Reply With Quote
Old 09-11-2004, 08:17 PM   #5
jascha
Prospect
 
Join Date: Sep 2004
Posts: 9
brilliant - thank you. the script there works exactly as i hoped.

a question: what exactly does the "Automatic" setting do in the Processor Performance menu? Does it toggle between Highest and Reduced based on demand, or is it not quite that simple?

cheers,

j
__________________
using: PowerBook 12", 1.33 GHz, 768 MB RAM, OS 10.3.5
jascha is offline   Reply With Quote
Old 09-15-2004, 08:15 AM   #6
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Quote:
Originally Posted by jascha
brilliant - thank you. the script there works exactly as i hoped.

a question: what exactly does the "Automatic" setting do in the Processor Performance menu? Does it toggle between Highest and Reduced based on demand, or is it not quite that simple?

cheers,

j

I'm sorry but my G4 don't make use of these settings - so I don't know. Maybe posting the request under the system forum will get a better response.
bramley is offline   Reply With Quote
Old 11-13-2004, 08:07 PM   #7
cheese_probes
Prospect
 
Join Date: Nov 2004
Posts: 3
could you please post the script as the discussion page seems to have disappeared

thanks
cheese_probes is offline   Reply With Quote
Old 11-14-2004, 05:55 PM   #8
jascha
Prospect
 
Join Date: Sep 2004
Posts: 9
hm - i don't have the original script anymore, just a compiled little applet. since the source code includes my password, i don't want to post it, but if you can tell me how to get AppleScript code out of a compiled applet then i'll gladly post what there is to post.
__________________
using: PowerBook 12", 1.33 GHz, 768 MB RAM, OS 10.3.5
jascha is offline   Reply With Quote
Old 11-14-2004, 09:23 PM   #9
cheese_probes
Prospect
 
Join Date: Nov 2004
Posts: 3
i believe that unless you have made the script read-only, you can open it in script editor to see the script's code. however, if the script is read only, i do not know how you can pull out the code. one question though, did you use the pmset command because i found that on another discussion at apple's support forums.
cheese_probes is offline   Reply With Quote
Old 11-15-2004, 05:40 PM   #10
hohoman90
Prospect
 
Join Date: Nov 2004
Posts: 5
couldn't you open the script in a editor such as SubEthaEdit? im not sure though :P
hohoman90 is offline   Reply With Quote
Old 11-15-2004, 08:25 PM   #11
cheese_probes
Prospect
 
Join Date: Nov 2004
Posts: 3
i just tried to open a read only script in subethaedit, it shows (what looks like to me) a lot of jibberish, but i think that someone who knew a little bit more than i do could figure out what the script's purpose is, but not the original applescript code (that last part is speculation)
cheese_probes is offline   Reply With Quote
Old 11-15-2004, 11:35 PM   #12
hermasj
Triple-A Player
 
Join Date: Jun 2004
Posts: 77
A script that's saved as an application _WITHOUT_ the "Run Only" checkbox can be opened in Script Editor and you can view or edit it's source. You'll have to use File->Open or drag the applet onto Script Editor's icon (since if you try to double-click the Applet it will simply execute).

However, if it's been saved as an application _AND_ the "Run Only" checkbox was checked you can't recover the source code... Script Editor will just give you an empty window.
hermasj 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 06:02 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.