AppleScript in Yosemite can't enter numbers in keystrokes - applescript

On OS X Yosemite (Version 10.10.4)
the AppleScript
tell application "TextEdit"
activate
delay 1
tell application "System Events"
keystroke "ABC123def"
end tell
end tell
Results in
ABCdef
without the numbers "123".
The same is true for entering other numbers.
Bug or feature? ;-)

I found the solution!
"Mausebedienung" was activated in "Systemeinstellungen" - "Bedienungshilfen".
So every number was mapped to a mouse-movement.
I'm sorry but at I don't know the exactly english translations. I guess:
- System Preferences
- ...assistance
- Mouse ...

I had this problem and I had to use key codes for numbers.
Example: I needed to cd into a file containing the number 3.
I tried:
keystroke "cd ~/Workspace/reminder3"
keystroke return
This didn't work.
The key code for the # 3 key is 20. (https://eastmanreference.com/complete-list-of-applescript-key-codes)
So I removed the 3 and added the key code {20} line like so:
keystroke "cd ~/Workspace/reminder"
key code {20}
keystroke return
and it worked for me, I hope it works for you

Related

Applescript Occasionally Freezes Automator

I'm trying to use this applescript to modify a file path written to a text file, copy the modified path to the clipboard, set it as variable thePath, use it to pull a Google Drive link from a list, and then copy that link to the clipboard.
Running it within Automator, it sometimes works perfectly but other times it stalls while running this applescript and crashes textedit, and then eventually times out without any specific error messages. Are there any issues with my code that would be causing Automator to freeze?
Note that I've substituted the actual links with link1, link2, etc. for privacy reasons.
on run {input, parameters}
tell application "TextEdit" to activate
tell application "System Events"
key code 125
key code 123 using shift down
key code 123 using shift down
key code 123 using shift down
key code 123 using shift down
key code 123 using shift down
key code 123 using shift down
key code 123 using shift down
key code 123 using shift down
keystroke "x" using command down
keystroke "a" using command down
key code 51
keystroke "v" using command down
keystroke "s" using command down
keystroke "w" using command down
delay 2
end tell
set thePath to the clipboard
set myList to {"link1","link2","link3","link4","link5","link6","link7","link8","link9","link10","link11","link12","link13","link14","link15","link16","link17","link18","link19","link20","link21","link22","link23","link24","link25","link26","link27","link28","link29","link30","link31","link32","link33","link34","link35","link36","link37","link38","link39","link40","link41","link42","link43","link44","link45","link46","link47","link48","link49","link50","link51","link52","link53","link54","link55","link56","link57","link58","link59","link60","link61","link62","link63","link64","link65","link66","link67","link68","link69","link70","link71","link72","link73","link74","link75","link76","link77","link78","link79","link80","link81","link82","link83","link84","link85","link86","link87","link88","link89","link90","link91","link92","link93","link94","link95","link96","link97","link98","link99","link100","link101","link102","link103","link104","link105","link106","link107","link108","link109","link110","link111","link112","link113","link114","link115","link116","link117","link118","link119","link120","link121","link122","link123","link124","link125","link126","link127","link128","link129","link130","link131","link132","link133","link134","link135","link136","link137","link138","link139","link140","link141","link142","link143","link144","link145","link146","link147","link148","link149","link150","link151","link152","link153","link154","link155","link156","link157","link158","link159","link160","link161","link162","link163","link164","link165","link166","link167","link168","link169","link170","link171","link172","link173","link174","link175","link176","link177","link178","link179","link180","link181","link182","link183","link184","link185","link186","link187","link188","link189","link190","link191","link192","link193","link194","link195","link196","link197","link198","link199","link200","link201","link202","link203","link204","link205","link206","link207","link208","link209","link210","link211","link212","link213","link214","link215","link216","link217","link218","link219","link220","link221","link222","link223","link224","link225","link226","link227","link228","link229","link230","link231","link232","link233","link234","link235","link236","link237","link238","link239","link240","link241","link242","link243","link244","link245","link246","link247","link248","link249","link250","link251","link252","link253","link254","link255","link256","link257","link258","link259","link260","link261","link262","link263","link264","link265","link266","link267","link268","link269","link270","link271","link272","link273","link274","link275","link276","link277","link278","link279","link280","link281","link282","link283","link284","link285","link286","link287","link288","link289","link290","link291","link292","link293","link294","link295","link296","link297","link298"}
set the clipboard to item thePath of myList
return input
end run
You could do this with AppleScript, but it would be much more sensible to do this with a shell script, by way of the Run Shell Script action in Automator, selecting the option to pass the input "as arguments". The default shell in macOS is zsh, so stick with that.
The script is short and sweet:
( PATH=/usr/bin:$PATH
basename "$1" | bc
) 2>/dev/null
basename extracts last path component, which, in this case, is the name of the folder, even if the path ends with a trailing slash. The result of this command is then piped through to bc, which performs basic calculations: this is purely to parse the folder name as a number, so that "00007" simply returns "7".
How to make use of this value is entirely dependent on how and where you store the various links from which it selects. Here's one suggestion, which is reasonably simple:
This allows you to hard-code your 300 or so links into a single Automator variable, which a second shell script reads and returns the specific link associated (by line number) with the value obtained from the previous shell script. The link then placed onto the clipboard.

Replace text in an applescript using an applescript

I have several hundred lengthy applescripts to edit where I need to find and replace the following code snippet in various places in each script.
tell application "Adobe Photoshop CC 2015.5"
set myLayer to current layer of current document
if last character of mySport is "s" then
set contents of text object of myLayer to mySport & ""
else
set contents of text object of myLayer to mySport & "'s"
end if
end tell
I want to replace it with
tell application "Adobe Photoshop CC 2015.5"
set myLayer to current layer of current document
set contents of text object of myLayer to mySport & "'s"
end tell
Is there a way to write an applescript to find and replace several lines?
code screen grab
The second problem is how do I deal with the apostrophe contained inside the quotes?
You can probably tell that I'm an artist and not a developer or scripter! I tried to get an answer a while back but unsuccessfully and the problem is now become critical.
Many thanks in anticipation of an answer.
The best would have been to set this subroutine as a separate script library and call it it in each of your scripts. Doing so, only one change would be enough. I advice you to do this way for next time.
I dig to find a way to make change in a script, but that's not that easy. Script Editor as very limited capability for scripting. the work around is to use the GUI scripting, which means that any changes made by Apple in future versions may no longer work.
The script bellow simulate your keyboard action to search & replace CurString to NewString :
set myScript to "Users:imac27:Desktop:Testscript.scpt" -- path to your script
set CurString to "Set B to 2"
set NewString to "Set X to 5"
tell application "Script Editor"
open myScript
activate myScript
delay 2
tell application "System Events"
keystroke "f" using {option down, command down} --mode search & replace
keystroke tab using {shift down} -- got to search area
keystroke CurString -- set the search target
keystroke tab -- goto replace area
keystroke NewString -- set replace value
-- click on menu "Replace all " which is the 7th item of "Search" menu item (=item 14th of menu "Edit")
tell process "Script Editor" to click menu item 7 of menu of menu item 14 of menu 4 of menu bar 1
end tell
compile front document
save front document
close front document
end tell
This script opens the script, it does the search, replaces, clicks on "replace" menu, then it compiles new version, saves it and closes it. If you have many scripts, you must run it through a loop for each script.
I tested it OK with simple line : replace "Set B to 2" by new line "Set X to 5".
However, your issue is more complex because you want to replace several lines, not only 1. I did not found a way to set the search area with multiple lines. I tried with CR (13) or LF (10), but it does not work. May be someone has an idea for that part ?
Also, if you want to add a " in your search or replace patterns, you can use the following :
set Guil to ASCII character 34
Set CurString to "this is a " & Guil & "s" & Guil & " between quotes"
In this case, the CurString value will be : this is a "s" between quotes
I purchased Script Debugger from Late Night Software and it enables the script to access pieces of code and replace them. Mark Alldritt was amazing in the support he offered and the software is now my "first use" destination.
You are sure of your original script and the final script? In this case no hesitation to use xxd and sed below in hexadecimal script which you wrote you can test this script, no danger for your script. Naturally, you change your path and names at your convenience.
set thePath to POSIX path of (choose file)
tell application "Script Editor"
set doc to open thePath
save doc as "text" in POSIX file "/Users/yourname/Desktop/yourscriptold.txt"
close thePath
end tell
set scp to do shell script "xxd -p -c 100000 /Users/yourname/Desktop/yourscriptold.txt " & " | sed -e 's#74656c6c206170706c69636174696f6e202241646f62652050686f746f73686f7020434320323031352e35220a736574206d794c6179657220746f2063757272656e74206c61796572206f662063757272656e7420646f63756d656e740a6966206c61737420636861726163746572206f66206d7953706f727420697320227322207468656e0a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f727420262022220a656c73650a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f7274202620222773220a656e642069660a656e642074656c6c#74656c6c206170706c69636174696f6e202241646f62652050686f746f73686f7020434320323031352e35220a736574206d794c6179657220746f2063757272656e74206c61796572206f662063757272656e7420646f63756d656e740a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f7274202620222773220a656e642074656c6c#' > /Users/yourname/Desktop/yourscriptnew.txt"
set scp to do shell script "xxd -r -p /Users/yourname/Desktop/yourscriptnew.txt >/Users/yourname/Desktop/yournewscript.txt"
do shell script "osacompile -o " & "/Users/yourname/Desktop/temporyname.scpt" & " /Users/yourname/Desktop/yournewscript.txt"
do shell script "rm -f /Users/yourname/Desktop/yourscriptold.txt "
do shell script "rm -f /Users/yourname/Desktop/yourscriptnew.txt "
do shell script "rm -f /Users/yourname/Desktop/yournewscript.txt "

How can I make my Applescript 'do' a script in my custom window?

I'm working on writing an Applescript that gets my Terminal ready for me to make Firefox add-ons.
tell application "Terminal"
do script "cd Public/addon-sdk-1.0"
do script "source bin/activate"
do script "clear"
end tell
When I run this script, my custom Terminal opens along with a regular Terminal window; and the bash script is ran in the regular window.. So, I'm trying to find out how to make the Applescript only open my custom Terminal, and execute the bash script in it.
The answer to your problem is to not use do script but to send keystrokes to your current terminal window with either keystroke or key code. Here's a script I use to do something similar. I just call this from the terminal with osascript myscript.scpt or launch it directly (I use LaunchBar for invoking applescripts) and it opens a new terminal tab (if the terminal is already open), gives it a custom name and then runs whatever commands I feed it. You could modify this to skip creating a new tab and just run in the current terminal window. I only use this approach when I have to do more than just run some standard terminal commands (such as send keys to an interactive python session), otherwise I just create a bash script.
global ENTER_, ESC_
set ENTER_ to 52
set ESC_ to 53
on run_commands(commands, pause)
tell application "System Events"
repeat with cmd in commands
keystroke cmd
key code ENTER_
delay pause
end repeat
end tell
end run_commands
on new_terminal_tab(tab_name)
activate application "Terminal"
delay 0.5
tell application "System Events"
# create new tab
keystroke "t" using {command down}
delay 0.5
# give it a name
keystroke "I" using {shift down, command down}
keystroke tab_name
delay 0.5
key code ESC_ # escape
end tell
end new_terminal_tab
new_terminal_tab("addon-sdk-work")
run_commands( { "cd /Users/username/Documents/dev/projname",¬
". env/bin/activate", ¬
"clear"}, 0.5)

Script delete line for xcode

I have some problem with the default script of xcode, here is my script to delete line :
on run
tell application "System Events"
keystroke (ASCII character 29) using {command down}
keystroke (ASCII character 8) using {command down}
keystroke (ASCII character 8)
end tell
end run
(command right ; command delete; delete)
The script work but, it do not delete the empty line when i use a keyboard shortcut ("command D" for me).
Please help me to make it work.
The problem is that when you press Command ⌘ + D, the script is executed and it presses the keys you've assigned. When the script tries to keydown backspace, you are still pressing the command key. So, it will actually press CMD+backspace.
Change the command key to option key in the shortcut. This way, you can delete the lines properly.

applescript oneliner

Is it possible to join applescript lines into one (in ruby it can be done using ;)?
Not really. The most that can be done is to take a simple if-then statement and make it into one line...
if (variable) then
return true
end if
...becomes...
if (variable) then return true
If you were to include the osascript command in a shell script, then multiple line scripts must delimited with -e...
osascript -e 'if (variable) then' -e 'return true' -e 'end if'
But that's about the extent of it. Applescript files aren't straightforward text files like most other programming languages (unfortunately) and we have to rely on its specialized editors for line management.
It depends on your code.
When you use AppleScript for GUI scripting, you can often write a bunch of nested tell blocks as one line.
For example these nested tell blocks:
tell application "System Preferences"
activate
end tell
tell application "System Events"
tell application process "System Preferences"
tell window "System Preferences"
tell scroll area 1
tell button "General"
click
end tell
end tell
end tell
end tell
end tell
They could also be written as:
tell application "System Preferences" to activate
tell application "System Events" to tell application process "System Preferences" to tell window "System Preferences" to tell scroll area 1 to tell button "General" to click
If you really want to avoid -e and force everything on one line you can push everything through echo
osascript -e "`echo -e \"tell application \\"MyApp\\"\nactivate\nend tell\"`"
Where " become \\" and newlines become \n.
I have re-arranged AppleScript from a block format, to a single line format as such:
Block format
tell application <application>
activate
open location <url>
end tell
Single line format
osascript -e "tell application \"<application>\" to activate & open location \"<url>\"";

Resources