Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   OS X - Development and Darwin (http://www.mac-forums.com/forums/os-x-development-darwin/)
-   -   AppleScript questions (http://www.mac-forums.com/forums/os-x-development-darwin/261332-applescript-questions.html)

jsherk 12-24-2011 12:50 PM

AppleScript questions
 
Hi

I am new to Mac (Lion 10.7.2), and am playing around with AppleScript. I have the following script that minimizes all visible windows:
Code:

tell application "System Events"
        --Get all the available applications that can be visible
        repeat with appProc in (every application process whose visible is true)
                --From the available applications above, click on the minimize
                -- button of every window that has a minimize button
                click (first button of every window of appProc whose role description is "minimize button")
        end repeat
end tell

Here are my problems and quesions...

The script does work, except it is kind of slow, meaning instead of instantly minimizing all the windows, you watch each one get minimized one by one. I tried saving it as an application from AppleScript Editor, but then it is really really slow (takes about 5 to 10 seconds before it even starts doing anything).

QUESTION #1
How can I create an alias on the desktop (to the .scpt file), that when clicked, will run the script instead of opening it up in AppleScript Editor? I do not want to the Script Menu in the menu bar to run the script.

QUESTION #2
Can this script be imported into either Automator or Xcode and be converted to an app that will run much faster?

QUESTION #3
I was looking for other ways to do this instead of "click button", and I found references to the miniaturized property of the window, but this property and the miniaturizable property both do not appear to exist when I view the properties of applications and windows. I have searched google for hours about this, and cannot figure out why it is not availble.

Thanks

jsherk 12-24-2011 02:31 PM

To answer my QUESTION #2 with regards to Automator, you can start Automator and choose New Application and then drag the Run Applescript Action over onto the right hand side and then paste your APpleScript in and save it and you now have an application that runs at the same speed as the applescript did and you can create an alias on your desktop. Would still like to know how to do it in xcode though!

Mark FX 12-24-2011 05:04 PM

I have done a fair bit of Applascript, and I can tell you there is no better way with Applescript.
As for Xcode it does not have any quick ways to do what you want, dvelopers did use the keystroke cmd-o, but this does not minimize windows, but hides them, this technique is not avised by apple, or me.
I have tried this simular script to yours, and it closed five apps in about one minute, so you must have a lot of apps running.
Code:

tell application "System Events"
        set visible_app_list to every application process whose visible is true
        repeat with visible_app in visible_app_list
                tell application process visible_app
                        activate
                        click (first button of every window of visible_app whose role description is "minimize button")
                end tell
        end repeat
end tell

You have to remember to have GUI scripting enabled in System Preferences.

Also bare in mind, that different apps take a different amount of time to quit

regards Mark

jsherk 12-24-2011 05:11 PM

Thanks for reply Mark FX ...

So in your experience, is the one draw back of AppleScript's that they typically do not run very fast?

Slydude 12-24-2011 11:24 PM

I'll defer to others with more experience with AppleScript if I am wrong. I don't think importing the script into Automator and saving it as an application will improve speed. It's been a while since I used the Script Editor but I believe it has a means of saving AppleScripts as applications directly without using Automator.

If the script editor is launching when you run your saved copy it has not actually been saved as an application. It is still running as a script. Here's how to save it as an actual application:

1. Launch the Script Editor and paste in or type in the script.
2. When the script is running properly choose Save As from the file menu.
At the next screen where you give it a name one of the options is file format and it is listed as "script". This is actually a drop down menu change the option to Application and save the file.

Once it is saved where you want it you can create an alias to this program just as you would to any other application.

jsherk 12-25-2011 12:37 AM

@slydude - Thanks for reply ...

Yes, I do know how to save script as application directly from script editor. The problem is (I don't know why) that when you run the .app file, it might take 5 to 15 seconds before it does anything, so this is obviously much slower than running the script directly from the script editor.

When I put the script in the Run AppleScript action in the Automator and save as an application, then when you run the .app file it starts right away with no delay. Using this method is NOT any faster than running the script directly from script editor, but is also not any slower either.

Thanks

Mark FX 12-26-2011 02:08 PM

Yes jsherk, a compiled script wether that is an app, or script, will be slightly slower than when in the script editor, because all compiled apps, have to do a certain amount of setup for themselves to be able to run.

Also when your script clicks on a GUI component of an other app, it basicically hands control over to that app, then returns control back to the script app, and then on to the next, so it wont be quick in any programming language, also Applescript is a interpreted garbage collected language, which also means its a slow language.

You could shut down or quit the other open apps, and this would be a lot quicker to perform, but thats not the same as minimizing them, and also any unsaved data in any of the running apps would result in save dialog boxes.

The only other advise I can give you, is to look at UNIX shell commands to perform the desired actions, but I dont have a lot of experience with UNIX commands, and so dont know if there are any utils for minimizing desktop apps.

Regards Mark


All times are GMT -4. The time now is 02:48 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.