Applescript keystroke wont work in BBEdit - applescript

tell application "BBEdit"
activate
tell window 1
repeat 100 times
repeat 5 times
key code 124
end repeat
--select insertion point after character 5
keystroke return
--keystroke key code 36
end repeat
end tell
end tell
gives error
error "BBEdit got an error: Can’t get keystroke \"
\" of window 1." number -1728 from keystroke "
" of window 1
All I want to do is emulate moving the cursor 5 spaces and press the return key lots and lots of time.
Seemed simple enough...

delay 0.2
tell application "BBEdit"
activate
tell window 1
repeat 100 times
repeat 5 times
tell application "System Events"
key code 124
end tell
end repeat
tell application "System Events"
keystroke return
end tell
end repeat
end tell
end tell

Related

Applescript for auto scrolling in Font Book. I could use some expert guidance

As a graphic designer, I am constantly having to identify which fonts to use in a document or match a font. Currently I have over 5000 different fonts. I don't keep all 5000 installed on my system. However, I usually do have to scroll through custom collections of 1000 fonts or more using the down arrow key. Sometimes the whole process takes me the better part of an hour
I created a script for auto scrolling in Font Book and saved it as an application named “FontBook_Auto_Scroll.app”. Basically it opens a dialog window giving me three options. If I select “arrow down”, it brings Font Book to the front and pushes the arrow down key 35 times in increments of half a second.
Then the dialogue window opens again. If I select “arrow up”, it brings Font Book to the front and pushes the arrow up key 7 times, etc. But here is the problem. If in the process of “scrolling down”, I see the font I want to use and it happens to appear as the second font in the “scrolling down” cycle, I would prefer not to have to wait until the 35 arrow key down entries are completed.
I'm still playing around with this script and making revisions as I continue reading AppleScript help documents. This is what I have so far.
property selectedFontFamily : missing value
tell application "Font Book"
activate
delay 5
try
set (selected collections) to font domain "AllFonts"
on error errMsg number errNum
set (selected collections) to font domain "AllFonts"
end try
try
set (selected font families) to font family 1
on error errMsg number errNum
set (selected font families) to font family 1
end try
end tell
tell application "System Events"
repeat 2 times
key code 48
end repeat
end tell
delay 1
repeat 40 times
activate
display dialog "Font Book Scrolling" buttons {"Arrow Down", "Arrow Up", "Cancel"} default button 1 giving up after 7
set the button_pressed to the button returned of the result
if the button_pressed is "" then
tell application "Font Book"
activate
delay 1
set (selected collections) to font domain "AllFonts"
tell application "System Events"
key code 37 using {command down, option down}
end tell
delay 1
set selectedFontFamily to (selected font families)
end tell
tell application "System Events"
delay 3
repeat 55 times
delay 0.6
key code 125
end repeat
delay 1
end tell
tell application "Font Book"
set selectedFontFamily to (selected font families)
tell application "System Events"
key code 37 using {command down, option down}
end tell
end tell
else if the button_pressed is "Arrow Down" then
tell application "Font Book"
activate
set (selected collections) to font domain "AllFonts"
tell application "System Events"
key code 37 using {command down, option down}
end tell
set selectedFontFamily to (selected font families)
end tell
tell application "System Events"
delay 3
repeat 55 times
delay 0.6
key code 125
end repeat
delay 1
end tell
tell application "Font Book"
set selectedFontFamily to (selected font families)
tell application "System Events"
key code 37 using {command down, option down}
end tell
end tell
else if the button_pressed is "Arrow Up" then
tell application "Font Book"
activate
set (selected collections) to font domain "AllFonts"
tell application "System Events"
key code 37 using {command down, option down}
end tell
set selectedFontFamily to (selected font families)
end tell
tell application "System Events"
delay 1
repeat 15 times
delay 0.7
key code 126
end repeat
delay 1
end tell
tell application "Font Book"
set selectedFontFamily to (selected font families)
tell application "System Events"
key code 37 using {command down, option down}
end tell
end tell
else if the button_pressed is "Cancel" then
tell application "Font Book"
quit
end tell
return
end if
end repeat
quit
end
on quit
tell application "Font Book"
quit
end tell
continue quit -- allows the script to quit
end quit
It's been my experience that once an AppleScript application starts running its script, sans coded exit points, the only way to get out of a loop is to force quit the application.
Because one may have more then one AppleScript application running at a time and the executable's name, regardless of what one named the application, is applet, you don't want to use a command like do shell script "kill -9 $(pgrep applet)", as it will kill all running AppleScript applications.
I'd have second AppleScript application handy, e.g. "Terminate - FontBook_Auto_Scroll.app", in the Dock for quick access, to isolate the PID of the target AppleScript application, using the following command syntax:
do shell script "kill -9 $(ps -x | awk '/[N]ame.app/{print $1}'); exit 0"
In the case of your "FontBook_Auto_Scroll.app", the command would be:
do shell script "kill -9 $(ps -x | awk '/[F]ontBook_Auto_Scroll.app/{print $1}'); exit 0"
The first character of the application's name is in square braces so as not to confuse the PID returned of the awk query that has the target AppleScript application's name in it.
The ; exit 0 it there so if you accidentally run the AppleScript application that terminates the target AppleScript application when it's not running, it does not error out.
Then when you want to stop the scrolling, use the "Terminate - FontBook_Auto_Scroll.app" AppleScript application to terminate the "FontBook_Auto_Scroll.app" AppleScript application.
BTW Looking at the coding of your AppleScript application, the issue you're going to run into is while it's in the loop, if you set focus elsewhere, the key code events are going to go to whatever has focus.
Update:
Here is some example code, using cliclick, to programmatically mathematically calculate, based on the properties of the UI Elements, where to click.
Tested under macOS 10.12.5, this code will click the All Fonts collection, then the first Font in that collection.
Note: Change the value of the cliclick variable based on where it's located on your system.
set cliclick to POSIX path of (path to home folder as string) & "bin/cliclick"
tell application "Font Book"
activate
-- delay 1
tell application "System Events"
set position of window 1 of application process "Font Book" to {0, 22}
set size of window 1 of application process "Font Book" to {800, 622}
set theFontBookAllFontsProperties to ¬
get properties ¬
of static text 1 ¬
of UI element 1 ¬
of row 2 ¬
of outline 1 ¬
of scroll area 1 ¬
of splitter group 1 ¬
of window 1 ¬
of application process "Font Book"
set theFontBookAllFontsPosition to position in theFontBookAllFontsProperties
set theFontBookAllFontsSize to size in theFontBookAllFontsProperties
set theXpos to (item 1 of theFontBookAllFontsPosition) + (item 1 of theFontBookAllFontsSize) / 2 as integer
set theYpos to (item 2 of theFontBookAllFontsPosition) + (item 2 of theFontBookAllFontsSize) / 2 as integer
tell current application
-- delay 0.25
do shell script cliclick & " c:" & theXpos & "," & theYpos
end tell
set theFontBookAllFontsFirstFontsProperties to ¬
get properties ¬
of static text 1 ¬
of UI element 1 ¬
of row 1 ¬
of outline 1 ¬
of scroll area 2 ¬
of splitter group 1 ¬
of window 1 ¬
of application process "Font Book"
set theFontBookAllFontsFirstFontsPosition to position in theFontBookAllFontsFirstFontsProperties
set theFontBookAllFontsFirstFontsSize to size in theFontBookAllFontsFirstFontsProperties
set theXpos to (item 1 of theFontBookAllFontsFirstFontsPosition) + (item 1 of theFontBookAllFontsFirstFontsSize) / 2 as integer
set theYpos to (item 2 of theFontBookAllFontsFirstFontsPosition) + (item 2 of theFontBookAllFontsFirstFontsSize) / 2 as integer
tell current application
-- delay 0.25
do shell script cliclick & " c:" & theXpos & "," & theYpos
end tell
end tell
end tell
Note: The delay commands may or may not be necessary and or may or may not need to have the value of the delay modified. Uncomment and set as appropriate to the needs.

Applescript: Repeat until reCaptcha occurs

I am pretty new in coding, especially with applescript. I managed to make the following code work:
tell application "Safari"
repeat
delay 7.5
set the URL of document 1 to "https://url.com"
delay 2
tell document 1
do JavaScript "document.getElementById(\"id1\").click()"
do JavaScript "document.getElementById(\"id2\").click()"
do JavaScript "document.getElementById(\"id3\").click()"
do JavaScript "document.getElementById(\"id4\").click()"
do JavaScript "document.getElementById(\"id5\").click()"
do JavaScript "document.getElementById(\"id6\").click()"
delay 0.25
end tell
tell application "Safari"
activate
end tell
tell application "System Events"
delay 0.25
tell process "Safari" to key code 48
delay 0.5
key code 21
end tell
delay 0.25
tell application "Safari"
tell document 1
do JavaScript "document.getElementById(\"book\").click()"
end tell
end tell
end repeat
end tell
Instead of repeating the code all the time I would like to make the code repeat until the Google reCaptcha occurs which pops up after the last javascript action.
My recommendation would be to restructure your repeat statement like this...
set done to false
repeat while not done
if reCaptcha = "something" then
set done to true
end if
end repeat

Applescript keystroke Filepath variable

I am trying to create a script that will upload products for me using Applescript and Automator. Im getting an error when the script tries to use the variable for the file path as it iterates through my list. The code is below. I am also inputing the contents of a folder from automator into this script. The error is on line 22. Also, if you could help with the second keystroke on line 42 which needs to iterate with the repeat but in a different folder. Thanks in advance!
on run {input, parameters}
repeat with theItem in input
tell application "Safari"
do JavaScript "document.getElementsByClassName('btn-com btn-info btn-large btn-icon-left add-product-button')[0].click();" in document 1
delay 1
do JavaScript "document.getElementsByClassName('btn-com btn-info btn-large btn-icon-left add-product-button')[0].click();" in document 1
delay 1
do JavaScript "document.getElementById('editor_commerce_product_short_description').innerHTML = 'All photos include full rights to the image and arrive via email. Comes in full 4K resolution. Image displayed is 1080P resolution. Orders may take up to 2 hours to be delivered. Watermarks will be removed!';" in document 1
delay 1
do JavaScript "document.getElementById('editor_commerce_product_price').value = '1.99';" in document 1
do JavaScript "document.getElementsByClassName('btn dropdown-toggle selectbox-display')[0].click();" in document 1
do JavaScript "document.getElementsByClassName('selectbox-content')[1].click();" in document 1
do JavaScript "document.getElementsByClassName('w-upload-input')[2].click();" in document 1
end tell
delay 1
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 1
keystroke theItem
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
delay 60
tell application "Safari"
do JavaScript "document.getElementsByClassName('w-upload-input')[1].click();" in document 1
end tell
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 1
keystroke "~/Desktop/Test_Samples/Test_1.png"
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
tell application "Safari"
do JavaScript "document.getElementsByClassName('save-product btn-com btn-success')[0].click();" in document 1
end tell
end repeat
return input
end run
It depends on the class of input / theItem. Keystrokes must be text, input can be anything.
Try
keystroke (theItem as text)
or
keystroke (POSIX path of theItem)
And in System Events the equivalent of ⇧⌘G is
keystroke "g" using {command down, shift down}

Apple Script that copies/pastes text is broken in Yosemite

I made a simple AppleScript that copies the layer names from Photoshop and pastes them into Illustrator. It worked fine in Mountain Lion but now it doesn't work properly in Yosemite. It repeats 6 times, but it doesn't seem to activate Illustrator on the first run through. It does activate Illustrator the other 5 times it repeats. Here it is:
repeat 6 times
tell application "Adobe Photoshop CC 2014" to activate
tell application "System Events"
tell process "Photoshop"
keystroke "/" using command down
keystroke "c" using command down
keystroke tab
end tell
end tell
delay 0.3
tell application "Adobe Illustrator" to activate
tell application "System Events"
tell process "Illustrator"
keystroke "v" using command down
keystroke return
keystroke "-"
keystroke space
end tell
end tell
end repeat
end
Thanks for any help!
tell application "System Events" to repeat 6 times
tell process "Photoshop" to repeat until frontmost is true
set frontmost to true
delay 1
end repeat
keystroke "/" using command down
keystroke "c" using command down
keystroke tab
tell process "Illustrator" to repeat until frontmost is true
set frontmost to true
delay 1
end repeat
keystroke "v" using command down
keystroke return
keystroke "-"
keystroke space
end repeat

Applescript run Safari in background

I have an applescript that is now doing what I want it to do: Open a specific URL, close the page and wait 2 seconds then do it again. The script works.
The problem is that when I run this on my machine, and I am trying to do something else at the same time, the Safari windows keeps popping up. I would really like to run this in the background so that I can continue to work on whatever I am doing.
The code I have is:
set theURL to "https://sites.google.com/site/whatever/"
repeat 100 times
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab --with properties {URL:theURL}
set URL of document 1 to theURL
on error
open location theURL
end try
delay 2
try
close window 1
end try
end tell
tell application "Safari"
quit
end tell
delay 1
end repeat
Anyone have a solution to this one?
Safari is coming to the front because you are activating it within the loop.
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to "https://sites.google.com/site/whatever/"
delay 2
close window 1
quit
end tell
delay 1
end repeat
EDIT
set myURLs to {"https://www.google.com/", "http://www.yahoo.com/"}
repeat with aUrl in myURLs
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to aUrl
delay 2
close window 1
quit
end tell
delay 1
end repeat
end repeat

Resources