applescript, included do shell script not working - applescript

I'm not a developer but try to use applescript to do some work for me.
I have a small script to find a application and kill it.
I have read many articles but not found a solution.
tell application "System Events"
set x to first process whose name is "Blotter"
return unix id of x
end tell
try
do shell script "kill " & x
end try
I get the process id as a result.
tell application "System Events"
get process 1 whose name = "Blotter"
--> application process "Blotter"
get unix id of application process "Blotter"
--> 34990
end tell
Ergebnis:
34990
But I'm not able to kill it...
I would be really grateful for a tipp. Thanks

Try this instead:
tell application "System Events"
set proc to first process whose name is "Blotter"
set procID to unix id of proc
end tell
try
do shell script "kill " & procID
on error errstr
display alert errstr
end try
by using return you' rent getting the unix id of the process, you're merely ending the script. Put the unix id in a variable, and then use that variable in your do shell script.

Related

how to pass single quote to applescript by zsh alias

i am use applescript to login ssh by gateway。
alias sshtoxxxx="osascript ~/workspace/script/applescript/fox/ssh_xxxx.applescript root#10.10.1.199 hFVDa4d\'vLe"
when password contains ' , it will can not login.
and the applescript is below also it is no use
on run argv
set arg1 to (item 1 of argv)
set arg2 to (item 2 of argv)
testFunc(arg1,arg2)
sshxxx(arg1,arg2)
end run
on sshxxx(target,password)
tell application "iTerm"
reopen
activate
tell the current window
create tab with default profile
tell the current tab
tell the current session
write text "clear"
write text "logfin the gate way host"
delay 0.2
repeat until "xxxxx" is in contents
end repeat
write text "clear"
set targetText to "ssh " & target
write text targetText
repeat until "password" is in contents
end repeat
write text password
end tell
end tell
end tell
end tell
end sshxxx
The question is not related to Applescript. Zsh doesn't care, what programming language the called executable is written in.
If you want to use an alias, your approach is correct. Applescript will see the password parameter as hFVDa4d'vL. However, for ease of debugging, I recommend using a Zsh function instead of an alias; here you have to drop the backslash:
function sshtoxxxx {
osascript ~/workspace/script/applescript/fox/ssh_xxxx.applescript root#10.10.1.199 "hFVDa4d'vLe"
}

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 "

wait for command line program execution before issuing new action in AppleScript

I am pretty new to AppleScript. I need a script that opens 3 iTerm tabs and executes 3 command line programs respectively. The first program terminates, while the last 2 run indeterminately.
Here is what I have:
tell application "iTerm"
activate
set next to (make new terminal)
tell next
activate current session
launch session "Default Session"
tell the last session
set name to "vagrant-db"
write text "cd ~/Workspace/vagrant-db; vagrant up"
end tell
launch session "Default Session"
tell the last session
set name to "next/core"
write text "cd ~/Workspace/next"
write text "/usr/local/bin/sbt \"project core\" \"run\""
end tell
launch session "Default Session"
tell the last session
set name to "next/web"
write text "cd ~/Workspace/next"
write text "/usr/local/bin/sbt \"project web\" \"~re-start\""
end tell
end tell
end tell
Problem is I need to wait for the first command line operation to end (vagrant booting up) before issuing the second and third. Is there a way to do it?
Not sure if it is possible, but maybe using "do shell script" command from applescript instead, e.g.
set response to do shell script "ls"
will return the contents of the root folder.
Another way (but a very ugly one) is to use 'delay'. E.g.
delay 5
Will delay for 5 seconds
solved with something like
--first program exec
set a to 0
repeat until (a = 1)
if (text of current session contains "ready") then
set a to 1
end if
end repeat
--second program exec
--third program exec
Rusty's answer worked for me. I'm adding this answer to provide detail requested by fusio (sorry, I'd rather have provided this as comment, but comments require more reputation points than adding a new answer).
Rusty's answer requires already being in the context of talking to the current window.
on wait_for(str)
tell application "iTerm"
tell current window
set a to 0
repeat until (a = 1)
if (text of current session contains str) then
set a to 1
end if
end repeat
end tell
end tell
end wait_for
Then, in your code, call:
my wait_for("Provisioners marked to run always will still run.")
The above argument occurs later than "ready".
This is still plenty ugly, but it works.

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)

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