How to reload a Safari Extension from the command line? - applescript

I need to "Reload" a Safari extension from the command line (and also build the package later).
How can this be done?
Why?
I'd like to build in one step - my code is in CoffeeScript and thus I'm compiling it anyway.
What have I tried?
Apart from googling hopelessly I tried using this AppleScript:
tell application "System Events"
tell process "Safari"
click the button "Reload" of window "Extension Builder"
end tell
end tell
Which errors out with:
System Events got an error: Can’t get button "Reload" of window "Extension Builder" of process "Safari".
And this variation:
tell application "System Events"
tell process "Safari"
tell button "Reload" of window "Extension Builder" to perform action
end tell
end tell
Which doesn't give an error but also doesn't actually do anything.

It may be that the button isn't "named" as you expect. Check out UI Browser in order to view an application's interface hierarchy for use with GUI scripting.
The "Reload" button is accessible with:
click button "Reload" of UI element "ReloadUninstall[NAME OF EXTENSION HERE]" \
of UI element 1 of scroll area 1 of window "Extension Builder"

Extension Browser's view hierarchy seems to have changed since the accepted answer was written. This script works for me:
tell application "System Events"
tell process "Safari"
click button "Reload" of UI element 0 of window "Extension Builder"
end tell
end tell
Note that Safari and the Extension Builder window need to be open for this to work

Related

AppleScript - how to close "Your computer was restarted because of a problem" popup?

How to close the "Your computer was restarted because of a problem" popup in AppleScript?
We use a VM to execute e2e tests and they often fail because of this popup. I would like to programmatically close it.
I tried listing the windows based on https://stackoverflow.com/a/26196136, but the popup is not included in the list
tell application "System Events"
tell application process "Diagnostics Reporter"
if (exists window 1) then click button "Ignore" of window 1
end tell
end tell

Applescript to open an application in full-screen mode?

I'm trying to program Alfred to open my Terminal, Sublime Text, and Chrome with a workflow.
I would like for my terminal to open normally as a window, but I've been trying to get Chrome and Sublime to open full screen.
I was able to get Chrome to open up in full screen mode with:
on alfred_script(q)
tell application "Google Chrome"
tell window 1 to enter presentation mode
end tell
end alfred_script
However, this did not translate to work with my Sublime Text.
What am I missing here?
Another way to do this assuming you have not changed the default keyboard shortcut for "Enter Full Screen" is simply to have System Events invoke that shortcut (⌃⌘F). As with the other approach I've seen to doing this (changing the value of AXFullScreen—see mklement0's answer here for a thorough discussion of this method), this requires making the relevant window active.
For instance, to toggle the full-screen state of the frontmost window in Safari, run:
tell application "Safari" to activate
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
As found here (i need an applescript to open safari in full screen an to hide the toolbar on mavericks). The make new document line prevents the can't get window 1 error by opening a new tab if one has not previously been opened.
tell application "Safari"
make new document
activate
delay 3
tell application "System Events" to tell process "Safari"
set value of attribute "AXFullScreen" of window 1 to true
end tell
end tell

How to close / cancel a pop-up modal / dialog window in Applescript

I'm trying to fix a particular problem in this Applescript I made: https://gist.github.com/jwmann/08daed8a905cfbf4ff96
Context:
It's an Applescript where you select a song in a VLC playlist, run the Applescript and it will trash the song from the original location.
The Problem:
The problem occurs when a user attempts to delete a currently playing, single song where the VLC playlist is the size of just the single song.
Now VLC will explode if you try to delete a song that it's currently playing. To workaround this I've made the script stop VLC from playing and trying again to delete it.
Now if this workaround happens, VLC will no longer be playing. In a multi-song playlist, this can't be annoying. So the script will continue playing VLC at the end of the script. However, after deleting the song from a single song playlist, it will remove the song from the playlist, leaving no songs and therefore no playlist.
So when the script tries to play, it'll open a new window / dialog / modal to allow the user to find something to play. This is something I don't want to happen.
What I'm trying to do:
I need a way to:
Detect the correct Window
Tell that window to Close
Information I've gathered:
This is the window I'm trying to Cancel
This is the data that Accessibility Inspector shows me about the window.
<AXApplication: “VLC”>
<AXWindow: “Open Source”>
Attributes:
AXFocused: “0”
AXFullScreen: “0”
AXTitle: “Open Source”
AXPosition (W): “x=993 y=276”
AXGrowArea: “(null)”
AXMinimizeButton: “(null)”
AXDocument: “(null)”
AXSections (W): “<array of size 1>”
AXCloseButton: “(null)”
AXMain: “0”
AXFullScreenButton: “(null)”
AXProxy: “(null)”
AXDefaultButton: “<AXButton: “Open”>”
AXMinimized: “0”
AXChildren: “<array of size 8>”
AXRole: “AXWindow”
AXParent: “<AXApplication: “VLC”>”
AXTitleUIElement: “<AXStaticText>”
AXCancelButton: “<AXButton: “Cancel”>”
AXModal: “1”
AXSubrole: “AXDialog”
AXZoomButton: “(null)”
AXRoleDescription: “dialog”
AXSize: “w=574 h=402”
AXToolbarButton: “(null)”
AXFrame: “x=993 y=276 w=574 h=402”
AXIdentifier: “_NS:40”
Actions:
AXRaise - raise
Things I've tried:
tell application "System Events"
close window "Open Source" of application "VLC"
end tell
tell application "System Events"
click button "Cancel" of window "Open Source" of application "VLC"
end tell
tell application "System Events"
cancel window "Open Source" of application "VLC"
end tell
tell application "System Events" to tell (window 1 of application "VLC" whose subrole is "AXDialog") to close
In every example, System Events or VLC (I tried both) can't seem to find the window "Open Source" even though it's clearly called "Open Source" in the inspector. It's not a sheet, it's a window. I don't understand why I can't find this window.
Thanks to #l'L'l for recommending me Script Debugger 5, it allowed to view the data of the windows in a readable way and even provide code on calling specific elements.
How I found out was I ran this code:
tell application "VLC" to activate
tell application "System Events"
tell process "VLC"
set myUI to every UI element
end tell
end tell
The reason being is that even though I knew the window's name, Applescript couldn't ever find it, so I needed a way to see everything.
Within that, I found this:
If you look at the metadata of this window, you'll see that it's SUPER broken.
No wonder Applescript could never find it, it's barely a window.
From that list, it provides the exact code to reference parts of the window, including, the "Cancel" button.
This is the code that cancels my window:
tell application "System Events"
tell its application process "VLC"
tell its window "Open Source"
click button "Cancel"
end tell
end tell
end tell
So searching every UI Element was definitely useful and Script Debugger 5 definitely helped.

Apple Script Error: Can't continue click

I'm trying to open a messaging application (it does not have an Apple Script Dictionary (command + shift + o)), click on text, and type into the text box, and hit send.
Pop up: Script Error - Telegram got an error: Can't continue click after the application becomes active.
Result Tab: error "Telegram got an error: Can’t continue click." number -1708
P.S., The messaging application is Telegram.
Apple Script:
tell application "Telegram"
activate
delay 1
click on text "chat name"
keystroke "some text"
//assuming this works because text box is the first responder when the chat opens.
click on text "Send"
end tell
If an application lacks an AppleScript dictionary, any command except the standard commands launch, activate, open, reopen and quit will throw an error.
The solution is GUI scripting: The built-in application System Events is the bridge to send mouse clicks and keyboard events to the target application.
I don't know the application Telegram at all, so this code might fail, but it might also be a starting point
activate application "Telegram"
tell application "System Events"
tell process "Telegram"
tell window 1
keystroke "some text"
click button "Send"
end tell
end tell
end tell
You have two choices for a 3rd party app that lacks an AppleScript dictionary.
Option 1:
Use System Events as described above to perform an action on an element, e.g. click a button, keystroke text into a field, etc. The trick is to identify the element in syntax that is recognized by Applescript. Besides UIElementInspector mentioned above, which can be confusing and occasionally wrong/incomplete, you can also run the following commands in a separate Applescript Editor. For example, to get all UI elements for the active window (window 1) in Telegram:
tell application "System Events" to tell application process "Telegram" to tell window 1
UI elements
end tell
To get all UI elements for the main menu bar in Telegram:
tell application "System Events" to tell application process "Telegram" to tell menu bar 1
UI elements
end tell
In each case the Result pane will display a comma delimited list of all available UI elements in that window or menu bar. Moreover, the syntax as listed is guaranteed to be recognizable by Applescript. Just identify the correct element and tell System Events to tell it what to do.
For example if you want to click the Menu item "Format" In TextEdit first run the following:
tell application "System Events" to tell application process "TextEdit" to tell menu bar 1
UI elements
end tell
Among the results in the Result pane will be the following:
menu bar item "Format" of menu bar 1 of application process "TextEdit" of application "System Events"
Convert that to Applescript, run the script and it will click the "Format" Menu:
tell application "TextEdit" to activate --you need TexEdit up and running to click its menu bar
tell application "System Events" to click menu bar item "Format" of menu bar 1 of application process "TextEdit"
For submenus, etc. you just iterate the process asking for UI elements for the submenu. GUI scripting is iterative and empirical.
Option 2:
Download the free Terminal/Command Line app cliclick which allows you to click on any point in the screen. The screen coordinates you want to click can be manually identified with your cursor by holding down command + shift + 4.

Applescript to click on a specific icon in the Mac Menu Bar

Sometimes I use PdaNet to tether using my iPhone. The desktop client for OSX is not as rich as the one for windows. One of the chief differences is, that the OSX does not allow to automatically connect to iPhone as soon as the latter is plugged in.
Would you know of a way using Applescript to click on the PdaNet icon on the Menu Bar and then select and click the 'Connect' option on it ?
Here is what the 'PdaNetMac' application's menu bar icon looks like:
I have looked at the following questions but am an applescript newbie and am not sure how to search for PdaNet's icon on the menu bar:
Click menu item on Mac OSX Lion using AppleScript
Applescript: on clicking Menu Bar item via gui script
Accessing dock icon right-click menu items with AppleScript
I have confirmed that 'Enable Access for assistive devices' is enabled.
Based on the second question above, Here is my current attempt at doing this:
ignoring application responses
tell application "System Events" to tell process "PdaNet"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "PdaNet"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Interestingly, the above script works for me fine when I change PdaNet to Flux.
Thanks!!
You were very close !!
I just downloaded the PdaNet application to test this, and the only edit I had to make to your script was change PdaNet to 'PdaNetMac` ( I was thinking that this is the Process Name and so used the process name displayed in Activity Monitor).
So this works for me:
ignoring application responses
tell application "System Events" to tell process "PdaNetMac"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "PdaNetMac"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Hope this works for you too !!
(Very useful script, btw. Cheers !)

Resources