How to Open new link in Safari with AppleScript? - applescript

I'm trying to create a workflow to click through a webpage to complete an action at the end. I currently have it working, however I'd like it to open the first "button" that it clicks in a new tab.
I've already tried telling in to open a new tab, but I don't know how to make it open the first button it has to click in a new tab.
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
clickClassName("settings", 0)
delay 3
clickClassName("Button Button--link Button--link--has-divider Button--course-settings validator_link", 0)
delay 2
clickClassName("Button Button--primary", 0)
I expect it to open the clickClassName("settings", 0) page in a new tab

Related

Apple script find and replace text

So, right now my code clicks on a textarea in safari then it adds a word using keystroke, however I was wondering if there is a way to find and replace a word in that area and also is there a way to replace a highlighted word without keystroke?
Current code:
Sorry its very very poorly formatted since I am pretty bad at applescript
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
to clickID(theId)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').click();" in document 1
end tell
end clickID -- All the code up to this point did is allow me to click on IDs and classes in safari.
tell application "Safari" to activate
tell application "System Events"
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit"})
end tell
end tell
end tell
delay 2 -- All the code up to this point from the last comment did is open a new tab in safari.
repeat 5 times
set randomlist to {"1","2","3","4"} -- List of numbers that will be randomly selected and put in the texarea
set randomword to some item of randomlist
clickClassName("tool-button wikiEditor-toolbar-spritedButton", 0) -- This clicks the bold button on the sandbox page
tell application "System Events"
tell process "Safari"
keystroke randomword -- Uses keystroke to put replace the text inside the bold formatting area
end tell
end tell
delay 1
clickID("wpSave") -- Clicks save
delay 2
tell application "Safari" to set the URL of the front document to "https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit" -- Goes back to sandbox and repeats the process
delay 2
end repeat
If you want to test this then put in your wikipedia username in the link and login into your account. Pretty much its a wikipedia bot that will just put stuff in my wikipedia sandbox

How to click button in AppleScript?

Writing an AppleScript to open Image Capture and click the Import All button.
tell application "Image Capture"
activate
tell application "System Events"
tell process "Image Capture"
click button "Import All" of group 1 of splitter group 1 of window 1
end tell
end tell
end tell
Image Capture opens but the script throws an error message saying it couldn't find the button "Import All".
Have followed advice on other threads on how to check the location in Accessibility Inspector and how to translate that to AppleScript instructions.
What's missing?
To get the button and group numbers, you have 2 ways: use the utility aplication provided by Apple in the developper toolkit "Accessibility Inspector" or use small scripts to find the number yourselves.
I prefer using script method. it is a bit longer sometime, but it always works. Just write a script with instruction get UI elements. Here is a small example of such script:
-- return lis of UI elements of active application and paste result in Excel
property tab : ASCII character 9
global T
-- to find last active application
tell application "System Events"
set frontmostProcess to first process where it is frontmost
set visible of frontmostProcess to false
repeat while (frontmostProcess is frontmost)
delay 0.2
end repeat
set secondFrontmost to name of first process where it is frontmost
set frontmost of frontmostProcess to true
end tell
set ActifProcess to secondFrontmost as text
tell application ActifProcess to activate -- set active the last actived application
delay 1
-- recursive loop to list all elements of active window
tell application "System Events" to tell process ActifProcess to set myWindow to front window
set T to ""
getUI(myWindow, 1)
set the clipboard to T
display dialog "Result is in clipboard. paste in Excel or text document"
on getUI(UIObjet, myLevel) -- get recursively the list of UI elements
set AppleScript's text item delimiters to {"//"}
tell application "System Events" to set UIliste to UI elements of UIObjet
repeat with oneUI in UIliste
tell application "System Events"
set UItext to ("Level=" & myLevel & tab & "Name=" & (name of oneUI) & tab & (description of oneUI) & tab & "Class=" & (class of oneUI) as string) & tab & "Title=" & (title of oneUI) as string
set UItext to (UItext & tab & "Value=" & (value of oneUI) as string) & tab
try
set UItext to (UItext & "Position=" & (position of oneUI) as text)
end try
set UItext to UItext & return
try
set NbSub to count of UI elements of oneUI
on error
set NbSub to 0
end try
set T to T & return & UItext
end tell
if NbSub > 0 then
getUI(oneUI, myLevel + 1) -- there are other sub UI elements, get them
end if
end repeat
end getUI
Copy this script in Script Editor. Make active the window/application you want to get UI elements. Make this script active and run.
The result is sometime not easy to interpret because developper of the application/window you're looking for may not have use UI element clear names or titles which describe what they are. Then you will have to look their relative position in the window.
The "import all" button is "button 3 of group 2 of splitter group 1 of window 1" for image capture version 6.6. Also, I prefer to use button number, instead of button name to make sure the script works with any language.
tell application "Image Capture"
activate
tell application "System Events"
tell process "Image Capture"
click button 3 of group 2 of group 1 of splitter group 1 of window 1
end tell
end tell
end tell
Please note that any next changes done by Apple on Image Capture will impact your script.

Choosing combo box item with AppleScript doesn't trigger item action

I'm trying to use AppleScript to click an item in a select box.
When clicking the 'More...' item manually with the mouse a standard OSX file chooser dialog opens up, but when I try to do it using AppleScript, the 'More...' item shows up as the chosen item for the select box, but no dialog shows up.
So far I've tried... (element names came from Automator recorder)
tell application "System Events"
click static text 1 of window 1 of application process "DYMO Word Addin"
-- combo box arrow
click UI Element 1 of combo box 2 of group 1 of window 1 of application process "DYMO Word Addin"
set labelsList to (list 1 of scroll area 1 of combo box 2 of group 1 of window 1 of application process "DYMO Word Addin")
set numLabelsInList to (count text fields of labelsList)
set theTextField to (text field numLabelsInList of labelsList)
if numLabelsInList > 1 then
repeat with z from 1 to (numLabelsInList - 1)
key code 125 -- down arrow
end repeat
end if
-- stuff I've tried
click theTextField
keystroke return
key code 36 -- return
set focused of theTextField to true
set value of attribute "AXFocused" of theTextField to true
perform action "AXConfirm" of theTextField
end tell
... and now I'm out of ideas.
After a bunch more testing, it turns out that the file dialog only opens when the combo box has focus, and that clicking the combo box arrow button and menu items doesn't actually give it focus.
http://lists.apple.com/archives/accessibility-dev/2006/Oct/msg00013.html
After trying all of the methods from that thread to give the element focus, even 'click at' didn't work.
https://apple.stackexchange.com/questions/40141/when-mousekeys-are-on-how-do-i-click-or-move-the-mouse-using-applescript#answer-40859
That answer recommends cliclick as another way to move and click the mouse, which worked.
So in the end I ended up with
click static text 1 of window 1 of application process "DYMO Word Addin"
set labelsComboBox to (combo box 2 of group 1 of window 1 of application process "DYMO Word Addin")
tell labelsComboBox
set {xPosition, yPosition} to position of labelsComboBox
set {xSize, ySize} to size
end tell
set {realXPosition, realYPosition} to {(xPosition + (xSize div 2)) as string, (yPosition + (ySize div 2)) as string}
do shell script "/usr/local/bin/cliclick m:" & realXPosition & "," & realYPosition & " dc:" & realXPosition & "," & realYPosition
-- combo box arrow
click UI element 1 of labelsComboBox
...
Here you can find someone with a similar question and the answer was also cliclick
https://discussions.apple.com/message/17662850#17662850

How can I make this ApplesScript execute instantly without a 13 second delay?

I was trying to make a shortcut on OSX Mountain Lion to "Share a file" with the LogMeIn Application as one does not exist. I opened Automator and created an Application under "watch me do". I clicked record and
navigated to the menu bar and clicked on "LogMeIn">"Share a file..." and clicked "Share a file" in that dialog box and stopped recording. I then copied the commands in Automator and pasted them in AppleScript editor, hoping to change a few things to make it execute faster. I chose 10x for the speed in Automator and it still takes about 13 seconds from the time I do the shortcut. If anyone knows how to change this AppleScript to make it instant, please feel free to change it.
Any help would be appreciated.
Thanks
-- Click the “<fill in title>” menu.
set timeoutSeconds to 2.0
set uiScript to "click menu bar item 1 of menu bar 1 of application process \"LogMeIn Menubar\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Share a file...
set timeoutSeconds to 2.0
set uiScript to "click menu item \"Share a file...\" of menu 1 of menu bar item 1 of menu bar 1 of application process \"LogMeIn Menubar\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Click the “Share a file...” button.
set timeoutSeconds to 2.0
set uiScript to "click UI Element \"Share a file...\" of group 1 of window \"LogMeIn\" of application process \"LogMeIn\""
my doWithTimeout(uiScript, timeoutSeconds)
on doWithTimeout(uiScript, timeoutSeconds)
set endDate to (current date) + timeoutSeconds
repeat
try
run script "tell application \"System Events\"
" & uiScript & "
end tell"
exit repeat
on error errorMessage
if ((current date) > endDate) then
error "Can not " & uiScript
end if
end try
end repeat
end doWithTimeout
That's really cool. I didn't know you could copy/paste those "watch me do" commands in AppleScript Editor. I think I'll find a use for that.
Anyway you may not be able to make your code "instant" because ui scripting just does that. However if I was writing your code as a regular applescript, here's what it would look like. Try it, maybe it will help. I can't test it because I do not have that application. Good luck.
NOTE: I made "Share a file…" as "Share a file" and an ellipsis character (option-semicolon). If you have problems try changing the ellipsis to 3 periods.
set timeoutSeconds to 2.0
tell application "LogMeIn Menubar" to activate
tell application "System Events"
tell process "LogMeIn Menubar"
click menu bar item 1 of menu bar 1
-- delay until it opens
set startTime to current date
repeat until exists menu 1 of menu bar item 1 of menu bar 1
delay 0.2
if (current date) - startTime is greater than timeoutSeconds then
error "Waited too long for menu 1 of menu bar item 1 of menu bar 1"
end if
end repeat
click menu item "Share a file…" of menu 1 of menu bar item 1 of menu bar 1
-- delay until it opens
set startTime to current date
repeat until exists group 1 of window "LogMeIn"
delay 0.2
if (current date) - startTime is greater than timeoutSeconds then
error "Waited too long for group 1 of window \"LogMeIn\""
end if
end repeat
click UI element "Share a file…" of group 1 of window "LogMeIn"
end tell
end tell

Applescript studio - how do I get every control in a window

I'm trying to enable or disable all the control in a window as the programme changes from interactive to non-interactive mode. How can I ask a window to give me all its contents?
every control of window "mainWindow"
doesn't work, nor does
contents of window "mainWindow"
Actually, I haven't been able to find any good documentation for interacting with menu items from interface builder at all. Things like how to set the contents of popups, and buttons and so on.
thanks
The way I do it at the moment is:
property onlineControls: {"maxLength", "speed", "accelerationSlider", "accelerationField", "showInfo"} --and so on, listing all the controls by name
on enableControls(theList, enableState)
tell window "mainWindow"
repeat with theControl in theList
set the enabled of control theControl to enableState
end repeat
end tell
enableControls(onlineControls, true)
I've made several lists of controls tht get turned on or off depending on the state the programme is in. But it has to be hard coded, which I don't see as being the best way.
tell application "System Events"
tell process "Adium"
get entire contents of window 1
end tell
end tell
This script will give you as result all contents of front window of Adium: butons of window, tool bars of window, buttons of tool bars, etc. Enjoy =]
I haven't been able to find a way to get all the controls in a window, but here's an example of interacting with the menu of a popup button:
tell menu of popup button "somePopupButton" of window "mainWindow"
delete every menu item
repeat with i in someItems
make new menu item at end of menu items ¬
with properties {title:i, enabled:true}
end repeat
end tell
Is the same script as "BoB1990" with the possibility of getting back the information given by get entire contents of window in a string of whom you can observe or modify all the items listed :
tell application "System Events" to tell process "Adium"
set this_info to {}
try
display alert ((get entire contents of window (x as integer)))
on error errMsg set theText to errMsg
set this_info to do shell script " echo " & theText & " | sed 's#System Events got an error: Can’t make ##g;s# into type string.##g'"
end try
set info to {}
set info to do shell script " echo " & this_info
display alert (info)
end tell

Resources