Change active finder tab - applescript

Is it possible to change the active Finder tab over applescript? I would like to create scripts to change the tabs with cmd-[0-9].
For Safari there is the possibility to set current tab to tab2 but that seems to be not the case with Finder.

Yes.
Actions supported by each app is defined in applescript Dictionary. Just because something work in one built-in app doesn't mean it will carry over to another.
Solution 1 is to call System Event and invoke keystroke for Previous Tab or Next Tab.
Solution 2: since you want to refer to tabs by its postion, use UI scripting. This way the scripting can switch to any tab that fits a certain criteria.
tell application "System Events" to tell process "Finder" to tell window 1 to tell radio button 1 to click

Related

How to click on a button in an app from the status bar with Apple Script

I have see how to click on menu items but this application does not have those. I was wondering how to click on the button Connect
I tried click at {818, 320} but it did not work
Here is the scrip, i was able to open the app with this
tell application "System Events"
tell UI element "Hotspot Shield"
tell menu bar item 1 of menu bar 2
click
delay 2
get the actions of button "Connect"
end tell
end tell
end tell
FWIW One would not normally use tell UI element "Hotspot Shield" rather use tell application process "Hotspot Shield", however, looking at this through Accessibility Inspector, I do not see a way to click the "Connect" button. Even using a Watch Me Do action in Automator, while it will record the process, it won't play it back successfully.
That said, here is a workaround solution that works for me on macOS Mojave using AppleScript and the third-party command line utility Cliclick.
Looking at the cropped screen shot in your OP and using the approximate center of the Connect button to be at 320 for the y axis and using AppleScript to get the position of the menu bar item for use as the approximate x axis, the following example AppleScript code should work for you too.
tell application "System Events" to ¬
tell application process "Hotspot Shield" to ¬
tell menu bar item 1 of menu bar 2
set xPos to first item of (get its position)
click
end tell
delay 0.5
do shell script "/Applications/cliclick c:" & xPos & ",320"
Note that the value of the delay command may need to be adjusted to ensure the Connect button is visible before the click event takes place using cliclick.
Adjust the path to cliclick as needed. I placed it in /Applications just for testing purposes.
The usual caveats, regarding System Preferences > Security & Privacy > Privacy, apply.
I am not affiliated with the developer of Cliclick, just a satisfied user of the product.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.

AppleScript to block Safari pop-up windows

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,...

Access context menu items by name using AppleScript

I'm trying to use AppleScript to click on context menu items in Logic Pro, preferably by simply providing the name of the menu item. It seems like this should be possible because I'm able to set up keyboard shortcuts for these context menu items using system preferences and providing the name of the command.
For instance, if you right click on the main editing window in Logic, a menu pops up with an option called "Add Audio File..." If I create a system preferences keyboard shortcut for Logic and give it this menu item name, it's able to execute just fine. I'd like to recreate this with a script. I'm familiar with accessing normal menu items using the hierarchy like so:
tell process "Logic Pro"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "Save"
but as far as I know, there's no way to access the context menu (right click menu) that I want like this. It seems there should be a way to simply access a non-menu-bar menu item by name since system preferences is obviously able to do so.
Logic pro is not scriptable so my suggestion would be that you set a keyboard shortcut in the system preferences then use system events to use said shortcuts.
for example to enter find mode (assuming there is a find mode since i don't own Logic Pro)
tell application "Logic Pro" to activate
tell application "System Events"
tell application process "Logic Pro"
keystroke "f" using command down
end tell
end tell
I don’t think you need to use the context menus. “Add Audio File…” is available in other parts of the Logic Pro X user interface. If you open the Project Audio window, there is an “Audio File” menu button with an “Add Audio File…” button in it. So this AppleScript will activate the “Add Audio File…” command:
tell application "System Events"
tell application "Logic Pro X" to activate
tell process "Logic Pro X"
tell menu bar 1
click menu item "Open Project Audio" of menu "Window"
end tell
delay 1
tell window 1
click menu button "Audio File" of group 1 of group 1
click menu item "Add Audio File..." of menu 1 of menu button "Audio File" of group 1 of group 1
end tell
end tell
end tell
One thing to keep in mind if distributing a GUI script is that the above script will only work in Logic Pro X running on a Mac set to US English (and maybe other kinds of English) because the names of the menus change if the system is set to another language. What you can do is replace the names in the above script with numbers, which is a totally experimental process, as far as I know. You have to try different numbers and see which continues to work.
So you may be able to replace:
menu button "Audio File" of group 1 of group 1
… in the above script with:
menu button 1 of group 1 of group 1
… and get the same functionality, and the script would work on any Mac. Or you may need to use “menu button 2.” Same goes for the other named items in the above script.
Also keep in mind that the user you distribute this script to has to give System Events permission to control their Mac in the Security pane of System Preferences or this script won’t work. That can be a giant obstacle to distributing GUI scripts. And if you save your script as an Application, you have to digitally sign it or it won’t run on other people’s computers, and that can be complicated.

How to automate clicking a specific button repeatedly with AppleScript?

I'm trying to write a script that will:
Open a particular program
Click on a button at a particular spot within the program window
Repeat the click a certain number of times with a specified delay between clicks
I'm planning to use iCal to schedule running the script, but what should the actual script look like? Could it be run on the background without making any windows visible?
Most native applications support UI scripting like this:
reopen application "Finder" -- open a default window if there are no open windows
tell application "System Events" to tell process "Finder"
repeat 3 times
click button 2 of window 1
delay 1
end repeat
end tell
If you get an error like "Access for assistive devices is disabled", enable access for assistive devices from the accessibility preference pane.
If you have Xcode, you can use Accessibility Inspector to inspect UI elements. Or use something like this:
reopen application "Finder"
tell application "System Events" to tell process "Finder" to tell window 1
{class, value} of UI elements of UI elements
description of UI elements
properties of some UI element
attributes of some UI element
value of attribute "AXFocused" of some UI element
actions of button 2
end tell
If click doesn't work, try perform action "AXPress", set selected to true, or set focused to true.

How do you programmatically press a toolbar button with AppleScript?

I hope this isn't too obvious, but I'd like to press one of the toolbar buttons within an application by means of AppleScript.
Background: The button doesn't have any menu item or keyboard shortcut. Thus, I can't activate it by any of those methods; I need to find an AppleScript way of actually 'pressing' the button.
I am going to expand a little on what #hced said.
I agree that click button 3 of tool bar 1 of window 1 works in many cases. However from OS to OS thic can be a moving target. My example is iTunes. In iTunes 10.5.8 the airplay button is 17 where in 10.7.5 it is 25. However, in both instances it is called "AirPlay"!
So I suggest using the developer kit, and getting this code from Apple:
http://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/Intro.html
Run the application and whatever you mouse over will get you all the info you need to make this work in your AppleScripts:
click (every button whose description is "AirPlay") of window "iTunes" of application process "iTunes"
In this case there should only be one button with this description, but you can use the tool to discover whether this is the case. If so, limit the scope!
Hope this helps!!
Bo
Ah, figured out that one myself, too. Seems I'm on a self-commenting spree here.
This example worked for me, referencing a specific toolbar button by means of its description attribute:
tell application "System Events"
tell process "OmniFocus"
click (every button whose description is "Contexts") of tool bar 1 of window 1
end tell
end tell
I'll leave it up for any commenters with an opinion about referencing every button whose descripton is "Contexts" (as per my example), to express those down below. But I presume there can't be multiple buttons with the same description, right?
Ok, that was simpler than I thought, once I found out how. (Googling it didn't leave much help at all, but an application called Prefab UI Browser did the trick finding out how this is done).
Example:
tell application "System Events"
tell process "OmniFocus"
click button 3 of tool bar 1 of window 1
end tell
end tell
I've got a follow-up question, though:
How do you reference a toolbar button by name/description? I know for a fact that the button has a description attribute with the value: "Contexts". But again, how do I reference it in the click statement?

Resources