Script to activate and enter details in System Dialog - macos

I am trying to write a script which can enter details in the system dialog. But i am not able to access it. Is there a way to access this? I tried getting the list of all the system dialogs and alerts but still I am not able to catch this.
tell application "System Events" to ¬
get name of every application process ¬
whose role description of window 1 is "system dialog"
system dialog

tell application "SecurityAgent"
activate
end tell
tell application "System Events"
tell process "SecurityAgent"
set value of text field 1 of window 1 to "Username"
set value of text field 2 of window 1 to "Password"
click button "Allow" of window 1
end tell
end tell

I am not sure following is what you asked for, but try yourself:
tell application "System Events" to tell process "SecurityAgent"
return name of every window
end tell

Related

OSX applescript - check if the application in focus is not finder, otherwise do not execute command

I am a bit rusty with applescript; trying to find out if the current application in focus is finder, and if it is, the program shall bail. Otherwise, it should perform a command.
The problem is that I can't find a way to check if the current app in focus is finder. Did search online and there are examples that show how to bring an app in focus, but I just want to retrieve the name of the app that has focus; and do a check to see if the script should bail out or continue. Something like this; although I have no command to check the application in focus.
tell application "System Events"
# check if finder is the process on focus
if "Finder" is in focus then
display dialog ("Finder is in front")
else
display dialog ("Finder is not in front")
end if
end tell
Every application regardless whether it has an AppleScript dictionary or not has a frontmost property.
So you can simply write (System Events is not needed)
if frontmost of application "Finder" then
display dialog ("Finder is in front")
else
display dialog ("Finder is not in front")
end if
Here is one way to check and see if Finder is frontmost:
Example AppleScript code:
tell application "System Events" to set frontmostVisibleApp to ¬
(name of every process whose frontmost is true and visible is true) as string
if frontmostVisibleApp is "Finder" then return
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.
This is the syntax you want:
tell application "System Events"
# check if finder is the process on focus
if frontmost of process "Finder" is true then
display dialog ("Finder is in front")
else
display dialog ("Finder is not in front")
end if
end tell
This will throw an error if the Finder isn't running, as I'm sure you know. That seems like an unlikely case, but if it's something you're worried about you can turn the code around:
tell application "System Events"
# check if finder is the process on focus
set frontProcessName to name of first process whose frontmost is true
if frontProcessName is "Finder" then
display dialog ("Finder is in front")
else
display dialog ("Finder is not in front")
end if
end tell

Applescript: how to click on checkbox only if is not already selected?

I want to enable option "Open in Low Resolution" for all ".app" files in the system.
This is my Applescript code:
on run argv
tell application "Finder"
set myFile to (POSIX file (item 1 of argv)) as alias
open information window of myFile
activate
tell application "System Events"
try
click checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info") of process "Finder"
end try
end tell
close information window of myFile
end tell
end run
i run it with:
sudo find / -name *.app -type d -exec osascript myScript.scpt "{}" \;
This work very good: it looks for all ".app" files, pass the file to the script, the script will open the "information window" of the file, clicks the "Open in Low Resolution" checkbox and finally it closes the window.
Problem: i need to click on the Checkbox ONLY if is not already selected.
I tried with this solution (and other similar):
Tick a checkbox only if it's not selected
Nothing happens. No errors.
System Events, Applescripts ecc... are allowed to run on Security And Privacy Settings
This does not works:
on run argv
tell application "Finder"
--set myFile to (POSIX file (item 1 of argv)) as alias
set myFile to (POSIX file ("/Applications/App Store.app")) as alias
open information window of myFile
activate
end tell
tell application "System Events" to tell process "Finder"
try
set theCheckbox to checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info") of process "Finder"
tell theCheckbox
set checkboxStatus to value of theCheckbox as boolean
if checkboxStatus is false then click theCheckbox
end tell
end try
end tell
tell application "Finder"
delay 1
close information window of myFile
end tell
end run
It simply does nothing.
The following sample (with an app Info Window opened):
tell application "System Events" to tell process "Finder"
set theCheckbox to checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info")
tell theCheckbox
if not (its value as boolean) then click theCheckbox
end tell
end tell
Will produce the following error:
Can’t make «class valL» of {«class chbx» "Open in Low Resolution" of
«class scra» 1 of window "App Store.app Info" of «class pcap» "Finder"
of application "System Events"} into type boolean
What can i do next?
Best regards.
I suspect the problem is that you've put everything inside a Finder tell block. The Finder is notoriously finicky about scripting; best to use it as little as possible.
Rearranging things to use System Events seems to fix the problem (though admittedly my old MacBook Pro doesn't have a retina screen, so I had to test this using a different checkbox). Notice that you don't need to activate the Finder or the information window; you can do this all in the background.
on run argv
set myAppPath to item 1 of argv -- should be a POSIX path
tell application "Finder"
open information window of file (POSIX file myAppPath as text)
end tell
tell application "System Events"
set appName to displayed name of disk item myAppPath
tell process "Finder"
tell window (appName & " info")
tell first scroll area
if (value of checkbox "Open in Low Resolution" as boolean) is false then
click checkbox "Open in Low Resolution"
end if
end tell
click (first button whose role description is "close button")
end tell
end tell
end tell
end run

Auto click a dialog button using AppleScript

I want to use AppleScript to auto login for a web by Safari.
When I open a URL and give username and password, if it is the first time or hasn't saved the password yet, it will show message: "Would you like to save this password?".
I want to use AppleScript to choose "Not now". How can I do that?
Here is my current code:
tell application "Safari"
open location "http://www.amazon.com"
activate
end tell
if page_loaded(60) then
say "Page Loaded"
tell application "Safari"
set loginURL to do JavaScript "document.getElementById('nav-your-account').getAttribute('href')" in document 1
open location loginURL
end tell
else
say "Page Failed to load or Safari didn't open"
end if
if page_loaded(60) then
say "Page Loaded"
tell application "Safari"
do JavaScript "document.getElementById('ap_email').value = 'mritjp'; document.getElementById('ap_password').value = 'mritjp'; document.forms['signIn'].submit();" in document 1
---HOW TO CONTROL TO CHOOSE "Not Now" in message "Would you like to save this password?" of safari
activate
delay 2
end tell
else
say "Page Failed to load or Safari didn't open"
end if
on page_loaded(timeout_value) -- in seconds
delay 1
repeat with i from 1 to timeout_value
tell application "Safari"
if name of current tab of window 1 is not "Loading" then exit repeat
end tell
delay 1
end repeat
if i is timeout_value then return false
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
delay 0.5
end repeat
end tell
return true
end page_loaded
Fixed by comment of Lauri Ranta:
If access for Assistive Devices has been enabled from System Preferences, you can use System Events:
on enabledGUIScripting()
do shell script "sudo touch /private/var/db/.AccessibilityAPIEnabled" password "1" with administrator privileges
end enabledGUIScripting()
Then:
tell application "System Events" to tell process "Safari"
click button 3 of sheet 1 of window 1
end tell
If access for assistive devices has been enabled from System Preferences, you can use System Events:
tell application "Safari"
do JavaScript "document.getElementById('login_button_id').click()" in document 1
end tell
delay 1
tell application "System Events" to tell process "Safari"
click button 3 of sheet 1 of window 1
end tell

AppleScript Clicking On dialog box

In PCSX, (ps1 emulator), i'm trying to automate the steps to play an iso. So, i'm doing this:
set thepath to path to me
set thesecondpath to POSIX path of thepath
set thethirdpath to "Contents/PSX/ROMS/img.bin"
set thefourthpath to "/Contents/PSX/PCSX.app"
set thefifthpath to thesecondpath & thefourthpath
set theultimatepath to thesecondpath & thethirdpath
tell application thefifthpath
activate
tell application "System Events"
keystroke "i" using {command down}
keystroke theultimatepath
delay 1.0
tell process "PCSX"
click button "Go"
end tell
key code 53
end tell
end tell
Running from the AppleScript Editor won't work. I made it to work running from the App it creates. PCSX and the img.bin are inside the Generated Package.
after pressing command+i, it opens a "Go to the folder" dialog, and i need to click Go and then Open
But doing this way, it won't find the dialog box. What am i doing wrong?
If Go and Open are the default buttons, try:
tell application "System Events"
keystroke return
delay 2
keystroke return
end tell
Although I don't have PCX installed here is an example of how to click the Go button from Finder's Go to Folder command.
tell application "System Events"
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
end tell
The reason your script won’t work from AppleScript Editor is that the “me” in “path to me” is the application that ran the AppleScript. When you are running the AppleScript in AppleScript Editor, that means AppleScript Editor itself. When you saved your AppleScript as a script application and ran it, the path to me pointed to your script application, because it was running its own AppleScript.
Also, this is incorrect:
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
The “Go” button is not on the window “Go to Folder.” It’s on a sheet which is attached to a Finder window which has the name of whatever folder is currently being viewed. So you have to describe the button as being on sheet 1 of window 1:
tell application "System Events"
tell process "Finder"
click button "Go" of sheet 1 of window 1
end tell
end tell
… but keep in mind that in another app, a similar looking button on a sheet may be on sheet 1 of group 1 of group 2 of window 3. UI Scripting is complicated.

Applescript setting ip address

I wanted to automate the process of changing my ip address using apple-script. So i wrote a script that will automatically do that, But i have problems in setting up the ip address.
set ipAddress to "192.168.110.48"
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.network"
end tell
tell application "System Events"
tell process "System Preferences"
click checkbox "Click the lock to make changes." of window "Network"
tell application "System Events" to keystroke "p"
tell application "System Events" to keystroke "a"
tell application "System Events" to keystroke "s"
tell application "System Events" to keystroke "s"
tell application "System Events" to keystroke "w"
tell application "System Events" to keystroke "o"
tell application "System Events" to keystroke return
click button 11 of window "Network"
tell window "Network"
tell sheet 1
tell tab group 1
click radio button "TCP/IP"
set contents of text field 2 to ipAddress
end tell
end tell
end tell
end tell
end tell
Everything executes fine except for this statement
"set contents of text field 2 to
ipAddress"
I am getting the following error:
error "System Events got an error:
Can’t set contents of text field 2 of
tab group 1 of sheet 1 of window
\"Network\" of process \"System
Preferences\" to \"192.168.110.48\"."
number -10006 from contents of text
field 2 of tab group 1 of sheet 1 of
window "Network" of process "System
Preferences"
I checked the UI elements using UIBrowser so i am sure that i am using the correct elements.
What is causing the problem? and Also can u tell a better way to write the same thing?
An alternative solution for changing IPs would be to use the network locations feature of the Network pane and use predefined locations.
If you click on the Network pane in Systems Preferences at the top you will see a pull down menu titled Location. Click that and choose Edit Locations...
Then you can create a new location and configure an interface whether it be ethernet, airport, firewire, 3G card, etc. You can have multiple locations for the same interface. So, you could have an ethernet location with an IP of 192.168.2.2 and then you could have another set to 192.168.2.3 and so on...
Once you have all of the new locations created and labeled then you can use AppleScript to toggle between them.
To get your current network location in AppleScript use the following code:
set currentLocation to do shell script "networksetup -getcurrentlocation"
To select a new location use the following AppleScript code:
set newLocationName to "whatever location you want to choose"
do shell script "scselect " & newLocationName with administrator privileges
Using the above method you can create numerous predefined network locations and toggle between them easily with your AppleScript. In addition, you could create a random function that will randomly select from an AppleScript list which is populated with all of your locations.
Use the networksetup shell script to manually setup your IP:
do shell script "networksetup -setmanual Ethernet 192.168.110.48
255.55.255.0 192.168.110.1 password YOURPASSWORD with administrator privileges"
To go back to DHCP:
do shell script "networksetup -setdhcp Ethernet YOURPASSWORD
with administrator privileges"
Of course you can manipulate the string to use variables and change "Ethernet" to any interface (like Wi-Fi).
This is a keyword error.
set contents of text field 2 to ipAddress is not a proper command
the right command is
set value of text field 2 to ipAddress
Enjoy yourself!

Resources