AppleScript to block Safari pop-up windows - macos

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

Related

xcode ios simulator: how to set focus on specific window

I have n instances of xcode ios simulator and I want to switch a focus on instance with specified id.
I tried next things:
Switching focus using "open": open -a Simulator --args -CurrentDeviceUDID <id>. But it opens the last focused simulator instance instead of the instance with passed id.
Switching focus using simctl tool. I tried to open URL in the specific instance of the simulator xcrun simctl openurl <id> "https://www.google.com", but it doesn't change the focus.
using applescript - it still focusing on the last focused simulator instance:
tell application "System Events" to tell process "Simulator"
perform action "AXRaise" of window 0
end tell
Do you have any idea, how to do this?
The following example AppleScript code is one method that works for me:
Example AppleScript code:
tell application "Simulator"
activate
set winName to the name of ¬
the first window ¬
whose visible is true ¬
and index is not 1
end tell
tell application "System Events" to ¬
perform action "AXRaise" of ¬
window winName of ¬
process "Simulator"
Notes:
The example AppleScript code assumes you have two Simulator windows visible, and it will toggle raising between the two. Obviously this is just an example and you'll need to modify and incorporate the methodology into you own code.
In your example AppleScript code, window n refers to its index as in:
index (integer) : The index of the window, ordered front to back.
    The quoted line above is from the AppleScript dictionary for Simulator in Script Editor.
The z-order of index starts at: 1
Using System Events and perform action "AXRaise" of window ... can be done by it's name property or it's index property.
The example AppleScript code, shown above, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional 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. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

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.

Allow JavaScript from Apple Events in Safari through Terminal Mac

I'm writing a program that executes do javascript in Safari. The only problem is that I'm trying to make the app give its self permission to do it. I'm trying to locate the file that handles the Safari developer preferences so that I can do this. Does anyone have any idea where this might be or how to change these settings?
It's in Safari's preferences plist at ~/Library/Preferences/com.apple.Safari.plist. The key you want is AllowJavaScriptFromAppleEvents. You can set it using defaults:
#to turn it on
defaults write -app Safari AllowJavaScriptFromAppleEvents 1
#to turn it off
defaults write -app Safari AllowJavaScriptFromAppleEvents 0
The virtual keyboard thing did not work for me. As StarPlayr at apple's develepoer forum has found out the problem is in something else.
For me problem occurred when i tried to do that on remote mac.
For some people plugging in a keyboard and mouse to the Server allowed to turn on JavaScript Apple Events in Safari and set the password.
However, for me that wasn't an option, so the next best thing is use an accessbility scripting feature and have the machine think a user is doing the clicks, allowing you to set the password:
-- The delays can be shorter, coordinates may vary
-- Best way to get the coordinates is with Apple screen capture (command-shift-3) from upper right to lower left (the coordinates will be shown)
-- if one spends the time, the click events can be converted to Accessibility AppleScript objects by capturing them as variables, or checking the events and using the events instead of the click coordinates
tell application "System Events"
tell application "Safari"
activate
end tell
delay 1
-- click develop menu (make sure its on first)
click at {430, 12}
delay 1
-- click Allow Javascript menu from Apple Events
click at {615, 615}
delay 1
-- Click the Allow Button
click at {1010, 386}
end tell

Change active finder tab

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

How do I scroll to the top of a window using applescript?

I want applescript to scroll a window all the way up.
I've tried the page up key, the home key, and I've tried looking for a way to scroll using the built in scrolling capabilities of the window, but I've so far been unable to even move the scrolled position at all.
Basically, use a tell app "System Events" statement to send keystrokes and key codes.
In theory, you could use the following:
keystroke page up key
keystroke page down key
keystroke home key
But for me this doesn´t work. The good news is that you can use the key codes instead. I suggest using the excellent free Full Key Codes application to read them, though it is a bit tricky to let it read two keys pressed simultaneously.
The key codes for the fn+ arrow keys-combos are as following:
Page up: fn+ up key: key code 116
Page down: fn+ down key: key code 121
Home: fn+ left key: key code 115
End: fn+ right key: key code 119
So for example if you had a long page open in Safari, and you want to scroll to its end, use
tell application "System Events"
tell application "Safari" to activate
— to see the animation, we wait a moment:
delay 0.5
key code 119
end tell
With browsers you could also use JavaScript:
tell application "Safari" to tell document 1
do JavaScript "window.scroll(0,0)"
end tell
tell application "Google Chrome" to tell active tab of window 1
execute javascript "window.scroll(0,0)"
end tell
The alternative to sending keystrokes is to use GUI scripting.
Caveat: While GUI scripting is more robust than sending keystrokes for a given version of an application, changes in the application's layout in future versions can break your code.
Also:
GUI scripting requires that access for assistive devices be enabled; enabling requires admin privileges:
up to 10.8, this could be done programmatically, system-wide by executing tell application "System Events" to set UI elements enabled to true (required admin privileges)
Sadly, on 10.9+, this no longer works, and apps must be authorized manually, individually - the system will prompt you on first run (requires admin privileges)
however, in both scenarios tell application "System Events" to get UI elements enabled will report whether access is enabled or not.
Determining the right UI element targets can be non-trivial and tedious; using the Accessibility Inspector utility that comes with Xcode helps. The class names reported by this utility correspond to the UI element classes contained in the System Events dictionary; e.g., AXSplitGroup corresponds to splitter group.
The following scrolls Safari 6.0.3's front window to the top (access for assistive devices must be enabled):
tell application "System Events"
# Use Accessibility Inspector to find the desired target.
tell front window of process "Safari"
tell scroll bar 1 of scroll area 1 of group 1 of group 1 of last group
set value of attribute "AXValue" to 0 # Scroll to top.
end tell
end tell
end tell
Update: As a reminder that this type of scripting works well for a given version of an application, the code had to be changed for Safari 8.0.4:
tell application "System Events"
# Use Accessibility Inspector to find the desired target.
tell front window of process "Safari"
tell scroll bar 1 of scroll area 1 of group 1 of group 1 of group 2
set value of attribute "AXValue" to 0 # Scroll to top.
end tell
end tell
end tell

Resources