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.
Related
I am working on an Automator Application (drag and drop) to rename some files. One of things I want to do is strip out the "where from" of the zip file. This script works, except it asks for the file when run vs allowing me to just drag/drop the file on top of the app icon.
What am I doing wrong? How do I make the application drag/droppable?
on deleteWhereFrom(fileToClean)
try
set posixPath to quoted form of POSIX path of fileToClean
do shell script "xattr -d com.apple.metadata:kMDItemWhereFroms " & posixPath
end try
end deleteWhereFrom
on open zip
repeat with i in zip
deleteWhereFrom(i)
end repeat
end open
on run
set zip to choose file with multiple selections allowed
repeat with i in zip
deleteWhereFrom(i)
end repeat
end run
In an AppleScript application, the open handler is used to get items dropped onto the droplet. In an Automator application however, items dropped onto the application are passed on to the first action in the workflow, which does whatever it does and passes its result to the next action in the workflow, and so on.
For the Run AppleScript action, its input is in the input parameter to the run handler, which is a list of items passed from the preceding action, and when the run handler finishes doing whatever it does, the result returned is what gets passed on to the following action.
In your original sample, the problem is that the Run AppleScript action is not using any of the items that are being passed along in the workflow, but is instead using choose file to ask for the items to use (again). Your script should be:
on run {input, parameters}
repeat with i in input
deleteWhereFrom(i)
end repeat
return input -- return the input items to any following actions
end run
on deleteWhereFrom(fileToClean)
try
set posixPath to quoted form of POSIX path of fileToClean
do shell script "xattr -d com.apple.metadata:kMDItemWhereFroms " & posixPath
end try
end deleteWhereFrom
Note that depending on any previous actions you are using, you may need to use a Set Value of Variable action at the beginning of the workflow to save the original items (the ones dragged onto the application), and then use a Get Value of Variable action (setting its "Ignore this action's input" option as needed) to get those original items in order to pass them on to the Run AppleScript action.
Just 3 lines of script to be able to test a droplet application without leaving applescript editor
set fich to POSIX file "/Appli/conv2spct.app" as string
tell application "Finder" to open POSIX file "/Users/yourusername/Desktop/somefile" using application file fich
If there are errors in your droplet a display dialog will be opened by the script editor applescript
The same script with choose file for the 2 elements
set fileappli to POSIX path choose file of type {"APPL"} with prompt "Choose a Droplet application to debug"--the droplet for debug
set fileargument to POSIX path choose file --the file argument to pass at droplet
tell application "Finder" to open fileargument using application file fileappli
If there are errors in your droplet a display dialog will be opened by the script editor applescript
Here's a pragmatic alternative using do shell script, which potentially allows you to specify multiple file arguments:
do shell script "open -a /Appli/conv2spct.app ~/Desktop/somefile1 ~/Desktop/somefile2"
The above paths happen not to need quoting (escaping) for the shell, but when using variables to specify the file paths, it's best to use quoted form of (to pass multiple arguments, apply quoted form of to each):
do shell script "open -a " & quoted form of fileappli & " " & quoted form of fileargument
Looks like this has become simpler since the question was first asked. According to this documentation you can write:
open {choose file}
on open theDroppedItems
...
end open
Run this from within the AppleScript editor and the file you choose will be opened as if it had been dropped onto the compiled script.
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.
I'm trying to build an AppleScript to launch my shell script.
Path structure is as follows
/Users/ryan/myscript/
applescript.scpt
bash.sh
My AppleScript is as follows:
tell application "Terminal"
set folder_path to path to me
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
activate
end tell
Problem is the 'path to me' is not always returning the correct path. When executed using the Mac cd/dvd autoplay behavior folder_path is equal to:
disk:System:Library:CoreServices:SystemUIServer.app:Contents:XPCServices:com.apple.systemuiserver.scriptrunner.xpc:
Is there is a better way of getting the folder path?
If this Script is in a static location, you can do this:
do shell script "/bin/bash" & POSIX path of (path to current user folder) & "myscript/bash.sh"
Path to me refers to the location of the applescript that is running. So if your script is on a disk then it will reference the location on the disk where the script is saved
if it is expected that the shell script will always exist in a folder called "myscripts" that exists in the current user folder then you could use path to current user folder and build out from there
set user_folder to path to current user folder
set folder_path to quoted form of POSIX path of (("" & user_folder & "myscript"))
tell application "Terminal"
activate
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
end tell
Is there a reason why you have to store the shell script in a separate file? Typically, you would put it inline, within the AppleScript code. As far as I know, the “do shell script” command only operates on text, not on a script at a file path. If you give it a variable that contains a path, it will try to run that path as a command. It won’t run the contents of the file as a command.
Here is an example of an AppleScript that runs an inline shell script and puts the results in TextEdit:
property theShellScript : "#!/bin/bash
echo Hello World"
tell application "TextEdit"
activate
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
… you can of course replace the above shell script with the contents of your own shell script.
If you do need to keep the script in a separate file, the best way to do that is probably to save your AppleScript as an Application, and put the shell script within the Application bundle. “Path to me” is the path of the application that is running the script — not to the script itself — but if you save your AppleScript as an Application, then it runs its own script, and “path to me” works as you originally expected.
Here is an example of an AppleScript that runs a shell script contained within a file that is stored within its own application bundle:
property theApplicationPath : the path to me as text
property theShellScriptPath : theApplicationPath & "Contents:Resources:Scripts:bash.sh"
tell application "TextEdit"
open alias theShellScriptPath
set theShellScript to the text of document 1
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
With the above script Copy/Pasted into a new document in AppleScript Editor, hold down the Option key and choose File ▶ Save As, and in the Save dialog box, on the File Format pop up menu, choose “Application” and of course give your application a name and click Save. Then in Finder, navigate to where you Saved your application, and 2-finger tap (or right-click) on your application and choose “Show Package Contents.” That opens your application up as a folder, exposing the file system within. Put your shell script file named “bash.sh” inside the folder “Contents/Resources/Scripts” within your application and then close the window that represents your application.
Now when you run your application from anywhere in the file system, it will still be able to find and run its incorporated shell script.
I am using Mac OSX 10.8.3.
I have a workflow in Automator set up that is as follows:
Ask for Finder Items
Get Folder Contents
Make Sequential
Move Finder Items
The purpose of the workflow is to automate renaming a bunch of photos that I have saved in folders then move them to an new folder.
I want to grab the foldername and stick it in a variable and use that variable in the "new name box" in the Make Sequential section of the work flow (see attached) image.
How do I grab just the folder name and assign it to a variable. My example has a variable called "FolderName"
Here is a screenshot
This works in testing:
Ask for finder Items - set type to Folders.
Set Value of Variable - this will be the path to the folder taken from action 1.
Run Shell Script - set the Pass input : as argument. And use /usr/bin/basename "$1" as the command line to get the folder name
Set Value of Variable - this will be the folderName of the folder taken
from action 3.
Get Value of Variable - set the Variable it obtains the value of to path
5a. set action 5 to ignore input from the above action 4 - Ctrl + mouse click on the action Title to get the Contextual Menu
Get Folder Contents - get the contents of the folder at the path passed on from Action 5
Rename Finder Items - Set to Make Sequential and new name = the folderName Variable
You can add your Move actions after as you wish.
You can use a Run Shell Script Action, with the following code:
echo ${1##*/}
and Pass input: set to as arguments.
This will filter the passed input, giving only the file basename as output.
You should put this step before the action that sets FolderName variable.
another short version to get a folder name and assign it to a variable.
tell application "Finder"
set currentDir to target of Finder window 1 as alias
set posixDir to POSIX path of currentDir
-- set myShell to "touch \"" & posix & "\"readme.txt"
-- set result to (do shell script myShell)
end tell