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!
Related
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
My end goal is to click the "Enable Switch Control" checkbox, which can be found under Accessibility-> Switch Control in System Preferences. I have had some trouble getting the applescript to work and would like to find out what is causing the issue.
My current code:
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
tell application "System Preferences"
reveal anchor "Switch" of pane id "com.apple.preference.universalaccess"
activate
end tell
I got the anchor Switch by using:
tell application "System Preferences"
get anchors of pane id "com.apple.preference.universalaccess"
end tell
which returned all of the available anchors. Here is a snippet:
...anchor TextToSpeech of pane id com.apple.preference.universalaccess, anchor Dwell of pane id com.apple.preference.universalaccess, anchor Dictation of pane id com.apple.preference.universalaccess, anchor Switch of pane id com.apple.preference.universalaccess, anchor Siri of pane id com.apple.preference.universalaccess...
I tried testing the applescript with other anchors (Dictation, Siri, etc) and they all worked (directed me to the intended area), but Switch doesn't. I even tried replacing "Switch" with its numerical value, reveal anchor 11 of pane id "com.apple.preference.universalaccess", but that also fails. Why is that? Also, how would I modify the applescript to get the intended result?
There appears to be a bug in that System Preferences will not properly reveal anchor "Switch" of pane id "com.apple.preference.universalaccess" and as such, here is a workaround.
Note that the coding in the example AppleScript code assumes that Switch Control has once previously been turned on manually so as to answer the initial System Preferences is trying to unlock Accessibility preferences dialog box where you have entered your User Name and Password then clicked the Unlock button.
The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue to click the Enable Switch Control checkbox.1
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
and works for me to click the Enable Switch Control checkbox:
-- # Check to see if System Preferences is
-- # running and if yes, then close it.
-- #
-- # This is done so the script will not fail
-- # if it is running and a modal sheet is
-- # showing, hence the use of 'killall'
-- # as 'quit' fails when done so, if it is.
-- #
-- # This is also done to allow default behaviors
-- # to be predictable from a clean occurrence.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
-- # Make sure System Preferences is not running before
-- # opening it again. Otherwise there can be an issue
-- # when trying to reopen it while it's actually closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
-- # Open System Preferences to the Accessibility pane.
tell application "System Preferences" to ¬
reveal pane id "com.apple.preference.universalaccess"
-- # Use System Events to achieve the goal.
tell application "System Events"
tell window 1 of application process "System Preferences"
-- # Wait for target pane to be available.
-- # The target in this case has a checkbox.
my waitForUIelement(checkbox 1)
-- # Ascertain the target row to select.
set rowSwitchControl to the first row of ¬
table 1 of scroll area 1 whose value of ¬
static text 1 of UI element 1 is ¬
"Switch Control"
-- # Select the target row.
select rowSwitchControl
-- # Wait for target checkbox to be available.
my waitForUIelement(checkbox 1 of tab group 1 of group 1)
-- # Click the Enable Switch Control checkbox.
click checkbox 1 of tab group 1 of group 1
end tell
end tell
delay 0.02
quit application "System Preferences"
-- ## Handler(s) ##
on waitForUIelement(uiElement)
tell application "System Events"
tell window 1 of application process ¬
"System Preferences"
set i to 0
repeat until exists uiElement
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
end tell
end tell
end waitForUIelement
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.
I am working on an Applescript to paste what was last copied to any current field. It is to be used with VoiceOver and the key code way (only way I know how) does not work all of the time.
tell application "System Events" to key code 9 using command down
say "paste"
I use keystroke:
tell application "System Events" to keystroke "v" using command down
I'm not aware of an error you should get, so you'll have to share.
There is a direct way to access the clipboard via the the clipboard keyword:
tell application "System Events" to keystroke (the clipboard as text)
Reference:
https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW28
tell application "myApp" to activate
tell application "System Events" to tell application process "myApp"
click menu item "Paste" of menu "Edit" of menu bar item "Edit" of menu bar 1
end tell
Obviously, replace myApp with the name of your app.
Advantages:
Only targets a specific app (safer!)
Will work even if the user set a custom keyboard shortcut for Paste
Text is pasted instantly (unlike using keystroke).
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.
I really like the new dictation feature of MacOSX Mountain Lion. I am using it in two languages; english (u.s.) and french.
Each time I need to switch language, I have to go in system preference, Dictation and Speech, and select the language.
Now, I would like use Applescript to do that automatically , unfortunately, since it is so new, I cannot get the proper string of dictation module.
Quick example (this is just a start):
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.xxxxxx"
end tell
for xxxx I tried "Dictation&Speech" a wild guess that did not work.
Any ideas on how I can get the exact string for "Dictation & Speech" ?
Thank in advance,
François
To get the id of a pane : go in system preference, select a pane, run this script in the Editor.
tell application "System Preferences" to get id of current pane
The result is the exact string.
You could either edit property lists that store the setting and reopen the DictationIM process:
#!/bin/bash
k="com.apple.speech.recognition.AppleSpeechRecognition.prefs DictationIMLocaleIdentifier"
if [[ "$(defaults read $k)" == en-US ]]; then
defaults write $k fr-FR
defaults write com.apple.assistant "Session Language" fr-FR
else
defaults write $k en-US
defaults write com.apple.assistant "Session Language" en-US
fi
killall -HUP DictationIM
Or use UI scripting:
delay 0.3 -- time to release modifier keys if the script is run with a shortcut
tell application "System Preferences"
reveal anchor "Dictation" of pane "com.apple.preference.speech"
end tell
tell application "System Events" to tell process "System Preferences"
tell pop up button 1 of tab group 1 of window 1
click
if value is "English (United States)" then
click menu item "French" of menu 1
else
click menu item "English (United States)" of menu 1
end if
end tell
end tell
quit application "System Preferences"
Cool stuff!
If you have 'Notifications Scripting' installed ( http://www.cooperative-fruitiere.com/notifications/index_en.html ), then you can even have a notification informing you about the language.
And by the help of FastScripts, you can assign a keyboard shortcut to this script.
-- Switch the language of Mountain Lion's dictation
-- Here, we just toggle between English and German
-- Needs 'Notifications Scripting' ( http://www.cooperative-fruitiere.com/notifications/index_en.html )
delay 0.3 -- time to release modifier keys if the script is run with a shortcut
tell application "System Preferences"
reveal anchor "Dictation" of pane "com.apple.preference.speech"
end tell
tell application "System Events" to tell process "System Preferences"
tell pop up button 1 of tab group 1 of window 1
click
if value is "English (United States)" then
set language to "German (Germany)"
else
set language to "English (United States)"
end if
click menu item language of menu 1
end tell
end tell
quit application "System Preferences"
tell application "Notifications Scripting"
set event handlers script path to (path to me)
-- The user info parameter is a record. The supported data types are text, integer, real, boolean, date, alias, file and POSIX file.
set dict to {theName:"Notifications Scripting", theVersion:"1.0", theScript:event handlers script path}
display notification "Dictation Language" subtitle "Switched to:" message language
end tell
using terms from application "Notifications Scripting"
-- This handler is called when a notification was delivered.
on notification delivered title aTitle subtitle aSubTitle message aMessage actual delivery date aDeliveryDate user info aDict
end notification delivered
end using terms from
The script provided doesn't work unless you put in a delay command. Also, it needs to be application process, not simply process. Instead of explaining where to make the changes, I'll just post the working code. [Please note, this was tested on macOS Catalina. If apple changes the System Preferences GUI in future versions, this code may not work.]
tell application "System Preferences"
reveal anchor "Dictation" of pane "com.apple.preference.speech"
end tell
delay 1
tell application "System Events" to tell application process "System Preferences"
tell pop up button 1 of tab group 1 of window 1
click
if value is "English (United States)" then
click menu item "Hungarian (Hungary)" of menu 1
else
click menu item "English (United States)" of menu 1
end if
end tell
end tell
quit application "System Preferences"
I now use Alfred to trigger this bash script. I assigned a hotkey to it and replicated it for the languages I speak, so I have three different keyboard shortcuts for my three languages. It uses a slightly modified script to the ones above (with a different pref, no longer requires delays, and immediately triggers dictation.)
k="com.apple.speech.recognition.AppleSpeechRecognition.prefs DictationIMNetworkBasedLocaleIdentifier"
if [ "$(defaults read $k)" != "de_DE" ]
then
defaults write $k "de_DE"
defaults write com.apple.assistant "Session Language" "de_DE"
killall -HUP DictationIM
-- Somehow I need to do twice the keyboard shortcut to start the dictation. This is the first one.
osascript -e 'tell app "System Events" to key code {63,63}'
fi
-- second one
osascript -e 'tell app "System Events" to key code {63,63}'