Wait for PID to exit using AppleScript - macos

I'm new to AppleScript but I have to recreate a batch file which I have written for Windows in OSX and AppleScript seems the best way to do this. Basically, the script will created by another program dynamically and then executed. The AppleScript simply needs to wait for a process, which I want to identify by its process ID, and display a message if the process is still running after a specific amount of time.
Is this possible, and if so, how?
Thanks in advance

I eventually found a solution to my given problem after searching online a bit more. Here is the AppleScript code I use to check if a process with a given id pid is running
tell application "System Events"
set runningApplications to (unix id of every process)
if (runningApplications contains (pid as integer)) is false then
-- process is not running
else
-- process still running
end if
end tell
This is just a snippet obviously. Personally I have the above the statement in a repeat loop, but this offers a solution to checking a process id (which is the unix id).
Hope this helps others

Here are some things to (hopefully) get you on your way:
shell scripts in terminal, and 'do shell script' command: don't know how well you know Unix, but you definitely want to go there, and
learn basics of bash. with some limitations, you can run shell
scripts via AS through the 'do shell script' command.
writing the script dynamically: osascript and osacompile will probably come in handy. see the man pages. osascript can execute
scripts or script text, and osacompile can (!) compile text into
script form (non-text form), among other things.
script waiting for/watching process: more shell script stuff, or using the Finder (what used to be called the Scriptable Finder!),
that is, the Finder's scripting capabilities (dictionary), like
tell application "Finder" to get name of processes. The shell version (which can be called via the 'do shell script' AS command)
might be "ps ax | grep Safari | grep -v grep | awk '{print $1}'" (taken from
stackoverflow post. I like it because it returns empty string if no
match). Depending on how your main script will run, learn how the
idle handler works in a script application, and how that differs from
using an xcode-built app (if that's the route you go), or just a
script.
displaying a message: 'display dialog' is the super simple method, complete with timeout ("gives up" after n seconds). (Sorry if this is
so basic I just insulted your intelligence :-) )
other: Check out (unless you're already wedded to a script editing environment) Smile. It's my primary script editor.

Related

Send multiple lines of quoted text to IPython in Terminal Window

I want to send a selected group of lines to my current ipython window from a texteditor (It's TextMate in this case, but that's largely irrelevant.) The script uses a bash call so it can accept the variable and then an Applescript call to push the code to the window.
This current script works, but it can only send a single non-nested line at a time. Is there a way to fix this so I can send multiple non-nested lines of code at once?
#!/bin/bash
QUOTED_TEXT=${TM_SELECTED_TEXT//\"/\\\"}
echo "$QUOTED_TEXT"
osascript <<- APPLESCRIPT
tell application "Terminal"
set currentTab to (selected tab of (get first window))
set tabProcs to processes of currentTab
set theProc to (end of tabProcs)
if theProc is not "Python" then
set currentTab to (do script "ipython")
end if
do script "$QUOTED_TEXT \n" in currentTab
end tell
APPLESCRIPT
I don't use either TM or ipython myself so can't provide an immediate answer to your exact problem, but here's some general thoughts on calling AppleScript from Terminal:
Never pass arguments to AS like that: it's a mis-quoting accident just waiting to happen. Wrap your AS code in an on run argv ... end run handler, then append your extra arguments to the osascript command when calling it in bash. osascript will then pass those arguments directly to AppleScript as a list of strings assigned to the argv variable. Safe and simple.
Rather than wrap your AS code in a bash script, just add #!/usr/bin/osascript at the top of your AS code, save it as a plain text file in an appropriate location (e.g. somewhere on your shell's $PATH, such as /usr/local/bin), then do chmod +x /path/to/script to make it executable. This will allow you to run it directly from Terminal.
If you want to access STDIN or environment variables directly within an AppleScript-based shell script, use the AppleScript-ObjC bridge to call NSFileHandle's fileHandleWithStandardInput()'s readDataToEndOfFile() and NSProcessInfo's processInfo()'s environment() respectively. To access ARGV, use an explicit run handler as described above.
By default, osascript automatically writes the value returned by the run handler to STDOUT; alternatively, you can write directly to STDOUT at any time via NSFileHandler (you can put a plain return statement at the end of run handler to ensure it returns nothing else). And osascript automatically writes the results of log commands to STDERR, and sets the return code to non-zero when your script throws an uncaught exception (e.g. use an error ERROR_STRING number ERROR_NUMBER statement to raise an exception directly in your AS code).
(BTW, I wrote a File library not long ago that includes a bunch of very nice handlers for writing AS-based shell scripts. I no longer develop or support it myself; however, various folks have already forked it, so if you do much AS+shell work you may find it a helpful source of AS code to cut-and-paste or even to use as-is.)

Execute a shell command on a file selected in the Finder

I'm a very novice and infrequent applescript experimenter. I've tried for several hours now to learn the individual applescript commands for the following task, but I always run into errors. Perhaps someone much more adept at applescript will find this task easy and quick, and for that I would be very grateful. Here is the task:
I want to be able to manually select a document or file within the finder and then execute the following unix command on that file. I would then store the script under Finder's "Services" menu. The unix command is:
srm -rfv -m path/filename
In my attempts, I assumed that a script that would open Terminal and execute the command would be the way to go, but just couldn't get anything to work. Thank you in advance to any good programmers who can whip out such a script for me.
My tip: Create such services using Automator!
Create a new Service in Automator
Choose "File & Folder" as Input and "Finder"
Add "Run shell script"
Choose "as arguments" as input
Change echo "$f" to your command srm -rfv -m "$f"
Save it as "Safe delete"
From now on, if you select a file inside Finder you will find the option "Safe delete" in the context menu.
Enjoy, Michael / Hamburg
Craig's comment is pertinent, but I am just focus on the script itself, not on the shell command. the script bellow must be saved as Application and each time you drop 1 or more file on its icon, the shell script command will be executed for each file :
on open myFiles
repeat with aFile in myFiles -- repeat loop in case you drop more than 1 file on the icon script
try
do shell script "srm -rfv -m " & (quoted form of POSIX path of aFile)
end try
end repeat
end open
Still make sure that in your shell command 'srm -rfv', the 'v' is necessary because this script will not display any thing ! I don't think so. also I did not display error during remove. what do you want to do with error (like write protect, ...) ?
Update: I missed that the OP wants to create an OS X Service that integrates with Finder. ShooTerKo's answer shows how to do that (and his solution doesn't even require use of AppleScript).
The only potentially interesting thing about this answer is that it demonstrates AppleScript commands to open a file-selection dialog, get the chosen file's POSIX path and run a shell command with it, with some general background information about executing shell commands with do shell script.
Try the following:
# Let the user choose a file via an interactive dialog.
set fileChosen to choose file
# Get the file's POSIX path.
set filePath to POSIX path of fileChosen
# Use the path to synthesize a shell command and execute it.
do shell script "echo srm -rfv -m " & quoted form of filePath
Note:
There's no explicit error handling; if you don't want the script to just fail, you'll have to add error handling (try ... on error ... end try) to handle the case of the user canceling the file selection and the shell command failing (unlikely in this case).
The shell command has echo prepended to it in order to perform a dry run (see its output in Script Editor's Result pane); remove it to perform the actual deletion.
quoted form of is important for ensuring that a string value is included as-is in a shell command (no risk of expansion (interpretation) by the shell).
do shell script will NOT open a Terminal window - it will simply run the shell command hidden and return its stdout output, which is usually what you want. If the shell command signals failure via a non-zero exit code, an error is raised.

Combining two scripts in applescript

I'm a complete newbie to applescript and my new external backup HDD made it necessary for me to work with it. As it is quite noisy I wanted to write a script that mounts the disk (if it is unmounted), runs the backup and then ejects the backup disk again (Code A). So far so good. In order to eject the disk after backup has finished I found a piece of code to check if a process is still running (Code B). It returns 1 if the backup process (backupd) is still alive and 0 if it is finished.
I am struggling now with combining those two pieces. I would like code B to keep checking after the backup has started if backupd is still running and if it is done go to the next step and eject the disk.
I just can't get code B running in code A and also the needed loop confuses me a bit. Any help is really greatly appreciated!! I can't imagine it's that tricky just too much for my imagination Thanks for helping me restoring peace and quietness
Code A:
set myVolumeLabel to "Time Machine"
tell application "Finder"
set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o > 'disk[0-9]*' "
if not (disk myVolumeLabel exists) then
do shell script "diskutil mountDisk " & diskDev
do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-> helper >/dev/null 2>&1 &"
(* Checking if the backupd process is still running should go here I suppose.*)
else
do shell script "diskutil eject /Volumes/'Time Machine' " & diskDev
end if
end tell
Code B
on check_process(marker)
set the_processes to (do shell script "ps -A")
return (the_processes contains marker)
end check_process
if check_process("/backupd") then
set x to "1"
else
set x to "0"
end if
---display dialog x buttons {"OK"} default button 1
Mac OS X (10.6.8)
It seems to me that what you want to do is rather low-level kind of "system stuff" and that more of the code should be done in the shell.
I'm learning both AppleScript and Unix shell-scripting (the bash shell to be precise, which is the default shell in OS X).
Most of your AppleScript here is really mostly shell scripts inside of AppleScript.
It seems like in this case, the right tool for the right job is a shell script.
You may not want to learn a whole 'nother programming language right now, so I'll give you a couple of thoughts.
If you end a shell-command with an ampersand "&" inside of the quotation marks, then AppleScript will NOT wait for the shell script to complete but rather it will return immediately, putting the process on a separate thread and will return a process id.
If you don't terminate a shell-command with an ampersand, then AppleScript will wait for the command to finish before proceeding.
You can try the following experiment. Type in the following command into Terminal.app:
sleep 10
It will take 10 seconds before you get control back in Terminal.
If you type the following command,
sleep 10&
You will get control back immediately and will get a process id back to refer to the process which you have started.
Well, again, it seems to me that the whole script is best written as a bash script, possibly using a little bit of Automator or AppleScript to kick things off.
Mac shell (bash) tutorial:
http://tidbits.com/article/7003
-- Kaydell
Let me be your guide

Shell Script & progressIndicator - AppleScript

What I have is an applescript app in xcode that runs a shell script. What I was hoping to do is have the progressIndicator move a certain amount when the command is "echo hello world" as an example but since all of my commands are sudo I have to put them in a shell script together and I can't just have the progressIndicator move in between commands (there are lots of them.) Is there a way to have the bar move when a certain command is started? Also, is there a way to output the log of the applescript to a textView in xcode?
You can always run the command line tool "ps" to see which processes are currently running. As such you can formulate a repeat loop and using ps can figure out which of your commands are currently running... and thus increment your progress indicator as needed.
Here's a ps command I've used to get a nice listing. You can combine this with grep to filter for your processes...
/bin/ps -Axcro user,pid,%cpu,command

Return Immediately from "run script" in Applescript

I want to be able to run an applescript from another applescript, but have it return immediately.
I cannot use "osascript script.scpt &" because osascript does not permit "user interaction" and I want to be able to.
So, I'm looking for the equivalent of: osascript script.scpt & in "run script script.scpt"
EDIT for Clarification:
I have an NSAppleScript object that runs a script - that's fine, but I want to initiate the script and immediately return, continuing with the rest of the Objective C program I'm writing.
Right now, the OBJC program waits until the NSAppleScript event finishes.
Probably the easiest thing is to use NSTask to fork osascript. If you can require Mac OS X 10.6, then you could also try using NSAppleScript in a separate thread, but note the limitations (this doesn't automatically make old language components and scripting additions thread-safe).
One way to do it is to ensure that the second script is launched as its own application. If the second script is static, just compile it once either with the Script Editor or osacompile and launch it from the first script. If you have to construct the second script on the fly, you could do something hacky like this:
osascript - <<EOF
-- first script ...
do shell script "osacompile -o /tmp/script2.app -e 'display dialog \"Hello, world!\"'; open /tmp/script2.app"
-- first script continues ...
EOF

Resources