Exit an Applescript after script execution - applescript

I am a total Applescript newbie - mostly I work by copying examples.
I have created a simple droplet that uses the image mounter built into Toast to mount disk images that are dropped on the droplet without actually launching the full Toast program. The droplet works, but I would like the script to quit once the disk images are mounted. (As things stand now, the script application becomes unresponsive shortly after the images are mounted, but sometimes it doesn't quit when the images are dismounted.) I searched the forum and figured out I'm supposed to do a "redirect" using > /dev/null 2>&1 &, but I can't get the syntax right.
May I please have some help - Thanks!
on open image
set mount to "/Applications/'Toast 11 Titanium/Toast Titanium.app'/Contents/MacOS/ToastImageMounter"
repeat with path in image
set mount to mount & space & quote & POSIX path of path & quote
end repeat
do shell script mount
end open
EDIT: I solved it, but I'm sure this isn't the most elegant solution, so I'd appreciate feedback.
on open image
set mount to "/Applications/'Toast 11 Titanium/Toast Titanium.app'/Contents/MacOS/ToastImageMounter"
set foo to space & "> /dev/null 2>&1 &"
repeat with path in image
set mount to mount & space & quote & POSIX path of path & quote & foo
end repeat
do shell script mount
end open

I'd change a couple things. First, why do you have single quotes in your path to the toast titanium mounter? It doesn't make sense because you want to quote the whole path, not just a small portion of it. Note that in applescript we have "quoted form of" to place the quotes properly around stuff so I used that in 2 places of your code. Second, the point CRGreen makes about variable names is valid so be careful just to avoid unnecessary issues. I'd change both mount and path. Finally, you want to add foo one time at the end of the command instead multiple times inside the repeat loop.
As such here's how I would write your code. Good luck.
set mountCMD to quoted form of "/Applications/Toast 11 Titanium/Toast Titanium.app/Contents/MacOS/ToastImageMounter"
set foo to space & "> /dev/null 2>&1 &"
repeat with thisPath in image
set mountCMD to mountCMD & space & quoted form of POSIX path of thisPath
end repeat
do shell script mountCMD & foo

Related

How to start a .command script from a mac automator app

I'm trying to create a mac "app" using automator that basically calls a .command file to do all the work. The command file will be in the same dir as the .app but i'm falling at the first which is - get the current directory of the .app file thats been clicked to determine the file location of the .command file.
i've tried
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
echo "-- $SCRIPTPATH"
This just returns my users director - basically ~
The app itself is in a dir on the Desktop example: ~/Desktop/foo/my.app
I've also tried
here="`dirname \"$0\"`"
echo "cd-ing to $here"
cd "$here" || exit 1
neither work.
ultimately i need to call my.command to run the command but need to know its actual position - or even relative to the app so that it'll fire. currently i get the error that it can't find the my.command as its not located in the root of my user account (since i wont have control over where it can be placed on the end users machine).
Any pointers on what i can do to solve this much appreciated.
Note: To answer - why am i using an app which has a terminal script to call a .command which is essentially a script - basically because if you do it this way a terminal doesn't actually pop up.. which for this demo is what i need to happen.
As you did not include explicit details of your Automator workflow, saved as an application, I'm presenting the following as an example of how to have and Automator app, e.g. my.app, execute the e.g. my.command script file, that which is located in the same folder as e.g. my.app is.
For the purpose of the example, I created a folder named foo on my Desktop, in which my.app was saved along with the my.command script file.
The Automator application workflow uses a Run AppleScript action to accomplish the goal.
Replace the default code with the following example AppleScript code:
set myCommandFilename to "my.command"
set myAppPathAlias to path to me
tell application "System Events"
set myDirName to POSIX path of container of myAppPathAlias
set myCommandFilePathname to myDirName & "/" & myCommandFilename
set myCommandFilenameExists to exists file myCommandFilePathname
end tell
if myCommandFilenameExists then
try
do shell script myCommandFilePathname's quoted form
on error eStr number eNum
display dialog eStr & " number " & eNum ¬
buttons {"OK"} default button 1 ¬
with title "File I/O Error..." with icon stop
end try
else
display dialog "A necessary file, ' " & myCommandFilePathname & ¬
"', is missing!" buttons {"OK"} default button 1 ¬
with title "Missing File..." with icon stop
end if
Note: Change my.command to the actual filename. The rest of the example AppleScript code should not need to be modified.
If my.app is launched and the my.command script file is not in the same folder as my.app, then an error message will be displayed, e.g.:
If my.app is launched and the my.command script file doesn't have its executable bit set, then this error message will be displayed, e.g.:
Also, if the my.command script file does not exit cleanly, it too will display an error message, e.g.:
The content of the error message will vary based on the content of the e.g. my.command script file, how it's coded and how it fails. This example is worst case scenario in that it lets you know something failed, but not what failed.
Note: The example AppleScript code is just that and does not contain any additional 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.

Change directory for screenshot from applescript

I am making a script that takes a picture of whoever opens it by taking a screenshot of PhotoBooth. However, I do not know how to change the directory of the screenshoot, because I just started AppleScript. For example, I want it to go the folder
/Users/ADMIN/Desktop/AllFolders/Folder1/Folder2
How would I do that?
Heres my code:
tell application "Photo Booth"
activate
set volume output volume 0
tell application "Finder" to set visible of process "Photo
Booth" to false
end tell
property N : 0
set N to N + 1
delay 0.1
set picPath to ((POSIX path of (path to desktop)) & "Picture_" & N & ".png") as string
do shell script "screencapture -tjpg " & quoted form of
picPath
tell application "Photo Booth"
quit
end tell
I finally figured it out without using iSightCapture!
First instead of making photo booth invisible, I close it immediately after taking a screenshot. Thanks #CJK for that bugfix.
To answer the problem, I found that the code:
set picPath to ((POSIX path of (path to desktop)) & "Picture_" & N & ".png") as string
do shell script "screencapture -tjpg " & quoted form of picPath
was the answer to this problem.
I got rid of the N addition to simplify it.
Instead of
POSIX path of (path to desktop)
I changed the path.
set picPath to (POSIX path of "/Users/ADMIN/Desktop/folder1/folder2/nameoffile ") & ".png" as string
This worked :)
Download isightcapture utility for Mac (Google search, thanks to axel#intergalatic.de to made this a freeware !), and store the file somewhere on your Mac, for instance in your Documents folder.
Use this shell command in Applescript via 'do shell script' instruction :
do shell script "~/Documents/isightcapture ~/Desktop/Photo.jpg"
The '~/Documents' is the path to your isightcapture command : here in your Documents folder.
The '~/Desktop/Photo.jpg' stores the picture made on Desktop with name Photo.jpg. Of course it could be any of your other folders !
There are several parameters for that command (isightcapture -h will give yo more options)
-t option to specify the photo format : jpg (by default) , png, tiff, bmp
-w and -h to define picture size
Do shell script ~/Documents/isightcapture -w 320 -h 240 -t png ~/Documents/Folder1/P1.png
This script take a photo and store it a png format in file P1.png in folder Folder1 in my Documents folder. The picture size is 320 x 240.
Note : this utility works since Snowleopard (OS 10.6) and I tested it up to OS 10.12. I don't see any reason why it would no work on next versions.

Make Shell script to wait until applescript finishes?

I have a shell script which calls an AppleScript. AppleScript runs some automated tests on a software with a given document.
After the AppleScript finishes its execution I am moving the document to some other folder using mv command in my shell script and then I am moving the next document so that AppleScript can run those tests with this new document. But as soon as AppleScript is called it moves the document to another folder without those tests being run on that document.
How can I give a wait in my shell script so that the document is moved only after the AppleScript has finished executing all the tests?
1. mv $file "/path/to/file" (Moving the document to the execution folder)
2. osascript /pathto/applescript.app (it will use this execution folder to run its tests)
3. mv "/path/to/file" /Users/Desktop/tempfolder (moving the document on which the test is completed to a temp folder)
Steps 2 and 3 go one after another without wait, hence the document on which the test is to be run is moved before the test completes.
Normally, shell commands are executed in-sequence. That is, the next statement is not run until the current one is done executing, whether or not it was successful. So if this is the case you can merely write your script one command after another.
I guess that osascript /pathto/applescript.app run some tasks in a different process, and that osascript returns from its own execution while the process doing "what you want" is still running.
What you could do would be to ps -aux | grep applescript.app then get the pid from that, and do a while loop afterward to see when the program cease. But that seem overengineered and you don't know the execution name of the applescript.app.
If I am wrong somewhere please anybody correct me
As Mayerz said, do shell script command is waiting for end of instruction to move to next AppleScript command. In any case, you can also use the "considering application response" bloc. Here is an example with a move in Finder :
set A to ("myVolume:Users:Me:Documents:sample.mov") as alias
set B to ("Users:me:Desktop:") as alias
tell application "Finder"
considering application responses
move A to B
end considering
end tell
display dialog "done"
Similar example with do shell script:
set A to ("myVolume:Users:Me:Documents:sample.mov") as alias
set B to path to desktop
set AU to POSIX path of A
set BU to (POSIX path of B) & "test2.mov"
tell application "Finder"
considering application responses
do shell script "mv " & (quoted form of AU) & " " & (quoted form of BU)
end considering
end tell
display dialog "done"

File associations in OS X: doesn't pass filename in args?

I am packaging an application into a .app directory for "drag install" or whatever it's called and I have a weird iessue with file association.
I set my application as a viewer for .xyz files, and the system does start my app when I double click that file; the only problem is that the path of the file I clicked is nowhere in the args[], there's only one parameter that is something like ~psn_0_901340 and I think is a timestamp because it changes every time.
So... what am I supposed to do? I've been sitting here for 2 hours straight and can't find a solution.
I think what you want is an AppleScript droplet.
A shortened version of the AppleScript from that link:
on open dropped_files
set the_command to quoted form of POSIX path of (path to resource "script.sh")
set file_list to ""
repeat with file_path in dropped_files
set file_list to file_list & " " & quoted form of POSIX path of file_path
end repeat
set the_command to the_command & file_list
do shell script the_command
end open
Export as an application using Script Editor. Place script.sh in the Resources folder.
Add your file extension associations to Info.plist. You may need to launch or move the droplet before OS X notices the change & allows you to double-click files.
If you want to launch Terminal or capture the script output, see the full AppleScript.

Delete file without playing sound in Applescript?

tell application "Finder"
set deletedfile to alias "Snow Leopard:Users:test.pdf"
delete deletedfile
end tell
The problem is I repeatedly call this script from my Cocoa application so the sound is played repeatedly too. Is it possible to disable that sound ?
Since the trash is just an invisible folder inside your home folder you can do this...
set myFile to (path to desktop folder as text) & "myFile.txt"
set trashFolder to path to trash folder from user domain
do shell script "mv " & quoted form of POSIX path of myFile & space & quoted form of POSIX path of trashFolder
one simple way (doesn't move to Trash)
do shell script "rm '/Users/test.pdf'"

Resources