Applescript droplet doesn't run another handler - applescript

I'm trying to make a droplet script in Applescript using the code below.
I have a handler which runs a command line with the POSIX path of the selected file.
When I run the script everything works (my command line handler works fine). But, if I drop the file to my script, nothing happens.
I need some help, please
on run
set thePath to POSIX path of (choose file of type "img" default location (path to downloads folder from user domain))
doIt(thePath)
end run
on open myImageFile
tell application "Finder"
if (count of myImageFile) is greater than 1 then
display alert "A message" as critical
else if name extension of item 1 of myImageFile is not "img" then
display alert "A message" as critical
else
set thePath to POSIX path of item 1 of myImageFile
doIt(thePath)
end if
end tell
end open

Change the line: doIt(thePath) in the open handler into: my doIt(thePath).
The reason for the error, is that you try to use your handler from within the scope of finder, and finder, doesn't reckognize it as something of its own. Therefore you must add myin front of it, so the handler can be resolved.

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.

Run script on a selected file

I'm trying to make script or automate unrar to unrar a selected file to a specific folder (hard coded).
I want the following code to be run in terminal by clicking a button in finder or a keyboard shortcut while I have a file selected.
unrar e <path_to_selected_file.rar> <hard_coded_path>
How can I do this in the best way?
If your destination path is hardcoded, then I suggest you to use Automator.
First create a Service. Select on top, "get the file" in application "Finder".
Then add only one action : "run an Applescript".
In that action, the default script starts with variable "input". This variable will contains the list of all selected files while you're doing a right click on them in the Finder. Build your script to loop through files of that list, using POXIS function to convert the finder path (myUser:myfolder:myfile) to shell path (myUser/myfolder/myfile). With this path, use the "do shell script" command to run your "unbar" script.
When saved and tested, you can also define a shortcut key for that Service (in System Preferences).
Here is the script which should be in your Applescript Action :
on run {input, parameters}
set Destination to path to desktop folder -- User Desktop by default. can be changed
set PosixDest to POSIX path of Destination
set SelectedFiles to input
repeat with myFile in SelectedFiles -- loop through each selected file
set PosixF to POSIX path of myFile -- convert Finder path to Unix path
try -- try block to handle error during unbar
do shell script "unrar e " & (quoted form of PosixF) & " " & (quoted form of PosixDest)
end try
end repeat -- next file
return input
end run
This example is running as long as you select compressed file (to accept the unbar command). To be more safe, you should just add a test to your file, to check if it is a file OK for unbar. If not, just do nothing.

Applescript to ask for input, verify folder exists, and then open folder

I want to write a script using Automator that opens a folder in a particular location, after receiving user input. Doesn't have to be Applescript.
So the steps would be:
Dialog asking for Folder name
Verifying the folder exists
If exists, open folder in new finder window
if not exist, display message
Any help would be greatly appreciated.
instead of asking user to type folder name into a dialog box, why not use the standard "choose folder" which provide usual file/folder selection graphic interface ? on top of that, it will also make sure the folder selected exists !
Also it is possible to user instruction "if My_Folder exists then ..." to check if folder (or file) exists
Example of direct user selection : 5 first lines are asking folder selection in folder Documents and detect user cancellation. Next lines are just example to display result
try
set SelectFolder to choose folder with prompt "choose folder" default location "/Users/My_User/Documents"
on error number -128
set SelectFolder to ""
end try
if SelectFolder is "" then
display alert "user did not select a folder"
else
display alert "User selection is " & SelectFolder
end if
The following script does exactly what you're asking for.
on run
set thisFolder to (choose folder with prompt "Choose a folder...")
if folderExists(thisFolder) then
-- display dialog "The folder exists." buttons {"OK"} default button 1
tell application "Finder" to open thisFolder
else
display dialog "The folder has been deleted" buttons {"OK"} default button 1
end if
end run
on folderExists(theFolder)
tell application "Finder"
try
set thisFolder to the POSIX path of theFolder
set thisFolder to (POSIX file thisFolder) as text
--set thisFolder to theFolder as alias
return true
on error err
return false
end try
end tell
end folderExists
That said, note that a folder that has been selected using AppleScript's Choose Folder command must always exist. It can't be selected if it doesn't exist. Therefore, my first thought is that you don't need this, but if you need to check whether a folder exist for a different reason, then the folderExists function will do the job.
You could use the following script to do what you're asking for (no choose folder, this script uses a dialog like you asked for):
set folderChosen to text returned of (display dialog "Enter the path to the folder you want to open:" default answer "")
try
do shell script "ls " & folderChosen
do shell script "open " & folderChosen
on error
display alert "Folder Doesn't Exist" message "The folder path you entered doesn't exist. Make sure to enter a path, e.x. /Users/USERNAME/MyFolder." as critical
end try
This AppleScript uses do shell script "ls " & folderChosen to verify if the folder exists, and then opens the folder if ls successfully runs, indicating the folder path exists. Otherwise, it displays an alert with a warning sign explaining the path they entered doesn't exist.
If you want this to run in automator, you could use Automator's Run AppleScript feature.
I hope this solution helps you solve your problem!

Applescript Droplet to Droplet Communication

I've created two droplets, one to rename files, another to print files. They are more complicated than that, but this is the essence. Sometimes we need to just rename them, sometimes just print them, and on occasion do both. I'd prefer to keep the two droplets separate because of extensive customization required for each user.
Desired workflow: drag file to RenameMe droplet, if command key is held down then pass the renamed file to the PrintMe droplet.
With the help of the checkModifierKeys script (sorry, don't have the citation handy) I can check to see if the command key is pressed, so that part of the script is taken care of. The problem is how to trigger the second droplet from the first. I've tried the opening the file with the second droplet as the application (as in the code below) but get a communication error.
Any ideas?
--Alex
Sample code:
on open the_Droppings
set flPth to POSIX path of (path to me) & "Contents/MacOS/checkModifierKeys"
set cmdPressed to (do shell script (quoted form of flPth & " command")) as integer as boolean
repeat with i from 1 to (count of items in the_Droppings)
set file_name to "NEW NAME FROM SCRIPT" #actual script that generates name isn't relevant
tell application "Finder"
set name of file (item i of the_Droppings) to file_name
end tell
if cmdPressed is true then
#pass the file to the PrintMe droplet
tell application "PrintMe"
open (item i of the_Droppings)
end tell
end if
end repeat
end open
You can add an explicit run handler to PrintMe, which will allow you two different entry points into the script. Both of which take arguments. I've set it up here with one file being passed to the run handler, and a list being passed to the open handler, but if you wanted you could pass a list to the run handler and repeat the same way you do in open.
In RenameMe:
if cmdPressed is true then
#pass the file to the PrintMe droplet
run script (load script file "path:to:PrintMe.app") with parameters (item i of the_Droppings)
end if
In Print Me:
on open the_droppings
repeat with i from 1 to (count the_droppings)
process(item i of the_droppings)
end repeat
end open
on run the_file
process(the_file)
end run
on process(the_file)
// Code for printing files goes here
end process

AppleScript Droplet on DMG not working

Hey I have the following AppleScript saved as a Droplet.
It is saved on a DMG file like this one http://dl.dropbox.com/u/1839051/TestDMG.dmg
The Problem is, while some can drag the template onto the Droplet and have it working, when I try to drag the template onto the droplet the crossed-out circle-symbol shows up indicating that this action is not possible. Nothing happens, the file is not copied.
Does anyone have any idea why I have this problem and how it can be fixed?
Thanks in advance guy.
on open thefiles
set outputFolder to (path to application support folder from user domain as text) & "iWork:Pages:Templates:My Templates:"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of outputFolder
tell application "Finder"
duplicate thefiles to outputFolder
end tell
end open
Rather than using a droplet and having the user to drag the files onto the droplet, why not just make an installer so the user only has to double-click the installer? It would be easier and also probably avoid your problem. I also added some error handling in your code because it's just prudent to do that with shipping code. We also tell the user what happened.
NOTE: you also had an error in your code. The outputFolder is a string. The Finder requires a file specifier. To make the string into a specifier you add either the word "file" or "folder" in front of the string path. Your code may have worked but the proper way to write it is with a specifier. Other applications may not take the string path but they will all take the specifier... so get in the habit of using them instead of strings.
try
-- create the output folder if necessary
set outputFolder to (path to application support folder from user domain as text) & "iWork:Pages:Templates:My Templates:"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of outputFolder
-- find the templates on the dmg disk
set myPath to path to me
tell application "Finder"
set myContainer to container of myPath
set templateFiles to (files of myContainer whose name extension is "template") as alias list
end tell
-- copy the templates to the output folder
-- NOTE: the script will error if any of the templates already exist
-- therefore we use a repeat loop and duplicate each file separately with a try block
-- around it to avoid errors in case some templates have already been installed.
tell application "Finder"
repeat with aTemplate in templateFiles
try
duplicate aTemplate to folder outputFolder
end try
end repeat
end tell
-- tell the user everything was OK
tell me to activate
display dialog "The templates were successfully installed! You may now use them in Pages." buttons {"OK"} default button 1 with title "Templates Installer" with icon note
on error
tell me to activate
display dialog "There was an error installing the templates. Please manually install them by copying them to the following folder." & return & return & (POSIX path of outputFolder) buttons {"OK"} default button 1 with title "Templates Installer"
end try
This looks to be a permissions issue, and I have to wonder if the differential between those who can and those who can't have something to do with which OS they are running. I'm running Mac OS 10.6 as an administrator and I was unable to perform the action in the DMG. But I was able to perform the action if I dragged both files out of the DMG and onto my Desktop.
If you need to install files in specific locations to the hard drive to support your project, then I would recommend making an installer (and a matching uninstaller as well) as opposed to the setup you have presented.

Resources