Pretty simple question:
Is there a possible way to do
repeat 10 times or until "Terminal" window exists
?
Try this.
repeat 10 times
if application "Terminal" is running then
tell application "Terminal" to if window 1 exists then exit repeat
end if
delay 0.1
end repeat
OR This
repeat 10 times
if application "Terminal" is running then
tell application "Terminal"
if windows exists then
tell me to beep 1 -- some optional command
exit repeat
end if
end tell
end if
delay 0.1
end repeat
Related
tell application "Terminal"
do script ""
set processList to processes of window 1
repeat with p in processList
set n to name of p
end repeat
activate
end tell
trying to get the process's name and it's failing help please
This will give you both the processes and names of each Terminal window.
set processList to {}
set windowNames to {}
tell application "Terminal"
set theWindows to windows
repeat with i from 1 to count of theWindows
set thisItem to window i's id
tell window id thisItem
set {end of processList, end of windowNames} ¬
to {processes, name}
end tell
end repeat
end tell
I am having trouble clicking on the "search" button on a particular website. The website is a subscription service, so I am attaching a picture of the page pulled up in "inspect" mode as well as my code.
My code:
set myURL to "https://www.uptodate.com/contents/search"
tell application "Safari"
activate
make new document with properties {URL:myURL}
end tell
tell application "System Events"
repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")
delay 0.3
end repeat
end tell
inputByID("tbSearch", "myVar")
clickClassName("newsearch-submit", 0)
###InputByID###
to inputByID(theId, theValue)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').value ='" & theValue & "';" in document 1
end tell
end inputByID
###ClickByClass###
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
As an alternative, you can use UI Scripting, where System Events preforms some keystrokes.
The following example AppleScript code works for me in macOS High Sierra1:
set myURL to "https://www.uptodate.com/contents/search"
tell application "Safari"
activate
make new document with properties {URL:myURL}
end tell
tell application "System Events"
repeat until (accessibility description of ¬
button 1 of UI element 1 of every group of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page") contains "Reload this page"
delay 0.5
end repeat
end tell
tell application "Safari"
do JavaScript "document.getElementById('tbSearch').value ='PPE';" in document 1
end tell
tell application "System Events"
keystroke space
key code 51 -- # Press the delete key to remove the space.
keystroke return
end tell
1 NOTE: For macOS Mojave and later, change UI element 1 to UI element 2 in the repeat loop above.
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. 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 played around with this and of course not having a subscription cannot truly test it but perhaps you can see if this works for you - it seemed to be working:
tell application "Safari"
--activate --can work with or w/o Safari in foreground
set myURL to "https://www.uptodate.com/contents/search"
make new document with properties {URL:myURL}
delay 5 -- just waits for (hopefully) the page to load
set d to do JavaScript "document.forms[0][0].value = 'joint pain'" in document 1
do JavaScript "document.forms['searchForm'].submit();" in document 1
end tell
I have a code that opens Safari to bing, inserts something to search, and searches it. However, I am trying to get the script to input the search THEN go down and search the autofill results rather than the actual search word. So far, I have been unable to do it. Here is a breakdown:
set theURL to "https://www.bing.com"
tell application "Safari" to make new document with properties {URL:theURL}
tell application "System Events"
repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")
delay 0.5
end repeat
end tell
inputByID("sb_form_q", "coronavirus")
delay 0.5
clickID("sb_form_q") -- Here is where it breaks down. Trying to "click" the search bar after pasting so that System Events can 'arrow down'to the autofill results.
tell application "System Events"
key code 125
key code 125
end tell
delay (random number from 5 to 15)
clickID("sb_form_go")
delay 0.5
tell application "System Events"
delay 1.5
keystroke "w" using command down
end tell
to inputByID(theID, theValue)
tell application "Safari"
do JavaScript " document.getElementById('" & theID & "').value ='" & theValue & "';" in document 1
end tell
end inputByID
to clickID(theID)
tell application "Safari"
do JavaScript "document.getElementById('" & theID & "').click();" in document 1
end tell
end clickID
Ultimately, goal is to randomly search the autofill results based of the input. The code so far does not work and will just search the original input text.
I did some small edits in your code. Now this version of code, I test it more than 40 times. Not one lose. I write a lof of comments in the code. You can test it on your machine.
set theURL to "https://www.bing.com"
tell application "Safari"
activate
delay 0.3
-- 0th edit: I find "activate" is nessary for testing the code
make new document with properties {URL:theURL}
end tell
tell application "System Events"
tell process "Safari"
--1st edit: here "tell process "Safari"" is a must on my machine.
repeat until exists (button "Reload this page" of UI element 1 of group 2 of toolbar 1 of window 1)
delay 0.5
--2nd edit: the original line in your post "repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")" doesn't work on my machine. If this doesn't work on your machine, you can change it back to your line.
end repeat
end tell
end tell
inputByID("sb_form_q", "coronavirus")
delay 0.3
--clickID("sb_form_q")
--delay 0.3
--3rd edit: I find this line is not nessary on my machine. Do you mean you use "arrow down" to show the autofill menu in your post? On my machine, after input "coronavirus", the dropdown autofill menu appears immediately and automatically. No need to use "arrow down" to show the dropdown menu.
tell application "System Events"
set randomNumber to random number from 1 to 8
repeat randomNumber times
key code 125
delay 0.3
end repeat
end tell
--delay (random number from 5 to 15)
clickID("sb_form_go")
--delay 0.5
--4th edit: actually i am not sure I understand what does the word "Randomly" mean in your post. I change the code here to select option in autofill randomly then execute the search.
tell application "System Events"
delay 2
keystroke "w" using command down
end tell
to inputByID(theID, theValue)
tell application "Safari"
do JavaScript " document.getElementById('" & theID & "').value ='" & theValue & "';" in document 1
end tell
end inputByID
to clickID(theID)
tell application "Safari"
do JavaScript "document.getElementById('" & theID & "').click();" in document 1
end tell
end clickID
My Safari version is "Version 10.1.2 (12603.3.8)" # MacOS 10.12.6. Let me know if it helps.
(First time with AppleScript...) I'm trying to bulk upload files from a local folder to a server via a single-upload form (legacy serverside software behind ddos wall, no control over it)
As I understand:
I can loop through each file in the filesystem.
With each file: Invoke "tell" Safari"
Invoke javascript to "click" a button by ID
file upload dialog, select the file to upload (?)
I'm having some trouble with syntax in implementing that...
(Also, if that's not the right/best approach, please provide a better one below!)
on run
tell application "Finder"
set mlist to (every file of folder "Macintosh HD:Users:username:filestouploadfolder") as alias list
repeat with this_file in mlist
tell application "Safari"
activate
do JavaScript "document.getElementById('selectToOpenFileDialog').click();" in document 1
choose file this_file
end tell
end repeat
end tell
return 0
end run
Hacked up a solution though it could probably be more elegant
on run
tell application "Finder"
set mfolder to "Macintosh HD:Users:yosun:png:"
set myFiles to name of every file of folder mfolder
end tell
repeat with aFile in myFiles
tell application "Safari"
activate
delay 1
do JavaScript "document.getElementById('addDeviceTargetUserView').click();" in document 1
delay 1
do JavaScript "document.getElementById('targetDimension').value=10;" in document 1
do JavaScript "document.getElementById('targetImgFile').click();" in document 1
end tell
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 1
keystroke "~/png/" & aFile as string
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
tell application "Safari"
activate
delay 1
do JavaScript "document.getElementById('AddDeviceTargetBtn').click();" in document 1
end tell
delay 10
end repeat
end run
I wrote a little applescript that attaches files in safari. Everything happens exactly how it would if I were attaching them manually, but for some unknown reason the attachments are not uploading even though they are being selected and submitted correctly.
I've spent a couple hours troubleshooting this and trying different variations with no success.
Here is the code that attaches them. I'm using cliclick in addition to applescript, which emulates mouse clicks.
set posix to "/Users/ea/Desktop/Guru/Deliverables" --set folder path
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke posix
delay 1
keystroke return
delay 2
keystroke "a" using {command down}
delay 5
do shell script "/usr/local/bin/cliclick m:381,339"
delay 3
do shell script "/usr/local/bin/cliclick m:818,590"
delay 2
do shell script "/usr/local/bin/cliclick tc:."
delay 2
end tell
files selected like they're supposed to
Upon clicking choose, nothing uploads.
Try This
tell application "Google Chrome" --Whatever your using
activate
set posix to "/Users/ea/Desktop/Guru/Deliverables/private" --are the files in here
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke posix
delay 1
keystroke return
delay 2
keystroke "a" using {command down}
delay 5
key code 36
end tell
end tell
Out of curiosity did you ever get it to work? and what do you mean by attaches files in safari? its select the files after you have the "file choosing window" open?
i know this is late, and someone seems to have already answered your question, but yeah you didn't respond to say if it worked or not, so heres something similar i created that might help:
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
to getValueFromClass(theclass, num)
tell application "Safari"
tell document 1
set theFirstTableHTML to do JavaScript "\n document.getElementsByClassName('" & theclass & "')[" & num & "].value"
return theFirstTableHTML
end tell
end tell
end getValueFromClass
on run
choose file with prompt "Which folder would like average file size calculated?"
open {result}
end run
on open theimage
--tell application "Finder" to set xx to every file in item 1 of theimage
--display dialog "Hey! the file's alias is: " & (path of theimage as string)
--display dialog theimage
set filepath to POSIX path of theimage
tell application "Safari" to open location "https://upload.vstanced.com"
delay 2
clickClassName("btn btn-big white outline", 0)
tell application "System Events"
activate application "Safari"
delay 0.5
keystroke "g" using {shift down, command down} --open goto
set the clipboard to filepath
keystroke "v" using {command down}
delay 0.7
keystroke return -- enter goto text
delay 0.4
keystroke return --press enter on file
end tell
delay 1
clickClassName("btn btn-big green", 0)
set thedirectlink to ""
repeat 15 times
set thedirectlink to getValueFromClass("r2", 1)
delay 1
if thedirectlink is not equal to "" then
exit repeat
end if
end repeat
set the clipboard to thedirectlink
tell application "Safari" to close current tab of window 1
display notification "Upload complete" with title "VStanced Upload" subtitle thedirectlink sound name "blow"
end open
This is a droplet (in script editor save it with "file format" as "application"), when you drag an image onto it, it opens up an image hosting site, uses javascript to open the "file choosing window", and then pretty much does exactly what yours does.
After it selects and enters the file, it also selects "direct link" from a little "toggle button" thingy, copies the direct link to the clipboard, closes the tab, plays sound and shows a notification. if you end up trying it and it doesnt work, lengthen the delays. it does everything (i'm 99% sure). But if safari's not already open it will probably take too long to get to the page and that will mess everything up. Im also going to try to add parts that "wait" for the page to load.