I'd like to use AppleScript to automate some of my common build tasks. But it appears under Xcode 4.2, scripting is so badly broken as to be useless. I cannot even run a minimal script such as the following (with Xcode running and my project loaded):
tell application "Xcode"
build project of active project document
end tell
This gives me the error "missing value". The scripting dictionary supplied is opaque to say the least. Nothing I have found on the Developer site helps, and searching for answers elsewhere indicates I am not the only frustrated one. Does Apple not want us to use AppleScript with Xcode 4? I'm using 4.2 as I don't want to upgrade from Snow Leopard to Lion yet.
UPDATE: specific questions are 1) how do I perform simple AppleScripting with Xcode 4 and 2) where is this documented?
Here is what I use to build & run on an attached iPhone. Even if an application does not support scripting, you can always just use menu commands. It's not what you want for a production environment but this is just supposed to be a little helper app of yours, right?
(* compile and let it run on iPhone *)
enabledGUIScripting(true)
activate application "Xcode"
tell application "System Events"
tell process "Xcode"
click menu item "Run" of menu 1 of menu bar item "Product" of menu bar 1
end tell
end tell
on enabledGUIScripting(switch)
tell application "System Events"
activate -- brings System Events authentication dialog to front
set UI elements enabled to switch
return UI elements enabled
end tell
end enabledGUIScripting
To answer part 2, Xcode scripting is documented in its scripting dictionary. In AppleScript Editor, choose "Open Dictionary…" from the File menu and select Xcode.
Related
I am trying to automate the blocking of pop-up windows in Safari.
I have tried the following defaults write operation
defaults write com.apple.Safari WebKit2JavaScriptCanOpenWindowsAutomatically -boolean false
But this fails to do it. I have also tried setting it as true but even that didn't help. I am currently trying to find a way to do it using AppleScript.
This is what I have written so far -
CMD_ACTIVATE='tell application "Safari" to activate'
CMD_NEWTAB='tell application "System Events" to keystroke "," using {command down}'
osascript -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB"
This opens up the preferences but then I draw a blank. Anyone with any suggestions on how to proceed? Also I don't really need a solution to this only in AppleScript any other way to do it would also be helpful.
Note: I have been using Mac OS only for a week now, and am not that well versed in the nuances of this OS, so please be a bit descriptive when answering.
Thanks.
Blocking pop-up window is done via Safari / preferences / tab Security where you need to set the correct checkbox.
This preference seems to be stored in your library / preferences file com.safari.plist which contains the flag com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptCanOpenWindowsAutomatically with value Yes when pop-up are blocked.
However, it may not be the only place to be changed and in any cases, it is not officially documented, so that place can be changed by Apple any time. This is not recommended to use it.
Going back to Applescript, because Safari is quite poor in terms of applescript handling events, you are forced to go via GUI scripting. That’s what you’ve started, but keep in mind that if Apple changes the layout of Safari preference window, your script must be reviewed.
When going through GUI scripting (which, again, should only be when no other solution found) you must understand structure of GUI objects. Window contains button, check box, tool bar... in a hierarchy model. For instance the preference window in Safari contains bellow the tool bar, an object "group 1" with itself contains many objects depending of the tool bar current selection. Once you understand that concept, the script below, which does what you're looking for, will be easy to understand with many comments:
tell application "Safari" to activate
tell application "System Events"
keystroke "," using {command down}
delay 0.2 -- leave sometime to open window
tell window 1 of process "Safari"
click button 6 of toolbar 1 -- Security button is number 6
delay 0.2
-- check if check box not yet set and set it.
if (value of checkbox 5 of group 1 of group 1) = 0 then click checkbox 5 of group 1 of group 1
end tell
delay 0.2
click button 1 of window 1 of process "Safari" -- click on red / close button
end tell
I am running on Safari 10.0.3. If your version is different, the preference window may be different. Then the script must be adjusted: the Security tab button may not longer be the number 6 in your version,...
I use 1Password to store my passwords, and fluid.app to create a few site-specific apps/browsers
While 1Password integrates well with Chrome, Safari and other browsers, it doesn't play nicely with Fluid apps, which is where my troubles lie.
I have therefore created a shortcut (with Keyboard Maestro) to run an applescript to open the mini, when I am in Fluid apps. However, I can't seem to get the applescript to launch the mini. Here are my attempts:
tell application "1Password mini" to launch does nothing, though AppleScriptEditor doesn't report any errors
tell application "1Password mini" to open nothing, though AppleScriptEditor doesn't report any errors
tell application "1Password mini" to activate is pointless, as 1Password mini is already activated
tell application "1Password" to open opens the main 1Password application, not the mini, which is what I need.
Interestingly, when I run (1), (2), and (3), AppleScriptEditor automatically replaces 1Password mini with 2BUA8C4S2C.com.agilebits.onepassword-osx-helper, which doesn't clarify things for me at all.
Does anybody have any clue how this can be solved?
I'm on OSX 10.10.2 (Yosemite), if it matters
I work for AgileBits, the developer of 1Password.
Looks like you found a solution, but here's another option for you:
open location "x-onepassword-helper://search/"
That AppleScript will open 1Password mini ready to search.
And if you're able to get the current website's domain from Fluid, you could even open 1Password Mini with a search term already filled in:
open location "x-onepassword-helper://search/twitter.com"
I hope that helps!
The solution was quite simple, with a little UI scripting:
tell application "System Events" to tell process "1Password mini"
tell menu bar item 1 of menu bar 1 to click
end tell
I have a problem using a simple AppleScript on Mac OSX 10.7.3.
With the following simple AppleScript which I find everywhere OSX raises an error 'The action "Run AppleScript" encountered an error'
I open up the Automator, create a Service, drop in a "Run AppleScript" node and enter the following code which I assume is correct because as I said it is the way a lot of people are doing it without any complaints.
AppleScript:
tell application "Terminal" to activate
tell application "System Events"
tell process "Terminal"
click menu item "New Window" of menu "Shell" of menu bar 1
tell application "Terminal" to close the front window
end tell
end tell
EDIT: When running in Automator I also get an error description:
Run AppleScript failes -1 error
Access for assistive devices is disabled"
Is Enable access for assistive devices enabled? If so, have you tried to reenable it?
Well, I guess I'll answer the question anyways (and thanks for editing your question to give a bit more useful detail).
Go to the "Universal Access" pane in System Preferences and at the bottom of the "Seeing" tab, you'll see a "Enable access for assistive devices" checkbox.
Turn that on and I suspect Automator will work.
I have my behaviours set up so that on successfully running a build, Xcode will open a custom debug window. I would like to then close this window when the run completes, however I cannot see an option for this. The best I can manage is returning focus to my main window without closing the debug window.
I have a two monitor setup and most the time use the second monitor for the Xcode organiser. Obviously the debug window is of more use to me when running the application, however I would like to have my organiser back on top afterwards.
Is there any 'Close Tab' behaviour or similar in Xcode 4.1?
Thanks
Update:
Just to say that I've filed a feature request with Apple. Since most of the other behaviours have options (a pop-down menu) to show/hide, it seems only natural that this should be an option for tabs/windows also.
I developed an Xcode plugin (Code on Github) that closes the debug window automatically after the debug session has ended. The plugin was developed and tested with Xcode 4.2.1 but should work with 4.1.
Usage
Download "Xcode Auto Close Debug"
Unzip it.
Move XcodeAutoCloseDebug.xcplugin to ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/
Restart Xcode
Open menu "Xcode" -> "Preferences" -> "Behaviors" -> "Run Starts".
Activate "Show tab" and set the tab name "XcodeAutoCloseDebug" (this exact name is important!)
Run the executable and detach the debugger window (drag the tab out of Xcode to create an own window).
Stop the executable and the window should close automatically.
... let me know if you experience any problems.
You can run a shell script when run completes (don't forget to make it executable) :
#!/bin/sh
osascript ~/Documents/close-xcode-tab.scpt &
And the I use the AppleScript Editor to create the scpt file :
tell application "System Events"
tell application "Xcode" to activate
tell process "Xcode"
tell menu bar 0
click menu item "Close Tab" of menu "File"
end tell
end tell
#keystroke "w" using {command down}
end tell
It will take 1-2 seconds to close the tab (but if you edit the script to only send the keystroke, it'll close quickly).
The limits are that we can't be sure to close the "good" tab as xcode4 don't let retrieve tab name (it's possible with safari).
I'm wondering how can I script applications which has no dictionary. All information that I've found tells me nothing. But my experience says me that there is a way.
For example:
tell application "Firefox"
return count of windows
end tell
Will work. And it will work with "Opera" and other applications without dictionary at all. So the questions are:
1) Why does it work?
2) What else work in such a manner? Is there a list of all such actions?
Thank in advance!
You can to some extent script applications without an applescript dictionary by using 'tell application "System Events"'
tell application "Keynote" activate
tell application "System Events" to keystroke "c" using {command down}
end tell
end tell
This example activates Keynote and then copies the current selection. You can use similar code in many applications even if they don't have an applescript dictionary provided you also have the "Enable access for assistive devices" option checked in the "Universal Access" System Preference.
Edit:
This document gives some details of how Cocoa provides some support for Applscript in all applications:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_about_apps/SAppsAboutApps.html
Under the heading Built-in Support for Standard and Text Suites
You can open the scripting dictionary of FireFox. Just open AppleScript Editor and go File → show dictionary, and choose FireFox from the list.
It just shows a very rudimentary dictionary. What happens is that the system library provides at least a basic set of commands (called the Standard Suite) to any Carbon or Cocoa app. This is what contains the definitions of window you used.
As Ian already wrote, in order to do more with such an app, you use the so-called UI scripting via System Events.
This nice website is a good place to start.