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

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.

Related

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.

Apple Script: how to delete a file

I am trying to delete a hidden file that shows up every time I restart my computer with an Apple Script set to run on startup. I can't however seem to be able to correctly guess the path of this file.
The file's path is Macintosh HD/Users/cristian/Dropbox (Hyperion)/Hyperion Team Folder/Icon
If I move the file to the desktop and run the script bellow, it works.
tell application "Finder"
delete the file "Icon
" of the desktop
end tell
My question is, how do I change this script to target the path above?
Also, is there anyway to permanently delete it not just move it to the trash?
Thanks in advance.
Assuming there is no new line character at the end of the file name this code deletes the file in the Dropbox folder and empties the trash.
Be aware that the empty trash command affects all items in the trash not only the currently deleted file.
set iconFile to ((path to home folder as text) & "Dropbox (Hyperion):Hyperion Team Folder:Icon"
tell application "Finder"
delete file iconFile
empty trash
end tell
Alternatively use the shell to delete the file, in this case the file will be deleted immediately.
set iconFile to POSIX path of (path to home folder) & "Dropbox (Hyperion)/Hyperion Team Folder/Icon"
do shell script "/bin/rm " & quoted form of iconFile
just use a do shell script command "rm" which delete file directly (without transfer to trash), like in script bellow :
Set myFile to "Macintosh HD/Users/cristian/Dropbox (Hyperion)/Hyperion Team Folder/Icon"
try
do shell script "rm " & quoted form of myFile
end try
However, it should be better to understand root cause why this file is added every time, and then address this root cause.

Applescript run bash script from same directory

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.

Applescript to Duplicate Files using My own application

I'm developing a Mac OS X application and in some case it needs to copy a file to /Library/ScriptingAdditions.
And using the code below
tell application "Finder"
duplicate sourcePath to destinationPath with replacing
end tell
will prompt a dialog saying "Finder" wants to make some changes...
I would like to make the dialog saying My Application wants to make some changes....
I've read about https://developer.apple.com/library/mac/documentation/security/conceptual/authorization_concepts/01introduction/introduction.html
but it doesn't seem to work with AppleScript.
If you would use the shell instead of AppleScript, the dialog asking for the password will display the name of your own application. Here's an example that copies the file "_this is a test.xyz".
set sourcePath to "'~/desktop/_this is a test.xyz' " -- mind extra space
set destPath to path to scripting additions folder -- change this to your destination folder
set destPath to POSIX path of destPath
set destPath to "'" & destPath & "_this is a test.xyz'"
set shellScript to "cp -n " & sourcePath & destPath
do shell script shellScript with administrator privileges
Warning: don't just run this script without modification, as it will add an empty file to your scripting additions folder and you probably don't want that. This script just serves as an example to look at.
Edit:
This will only work if you can compile your app as an independent app. If you're using Python, you need to compile your Python scripts as a standalone app with a name. The password dialog will show the name of the standalone app instead of "Python".

Remove multiple PDF passwords with workflow or applescript in a folder

How do I remove passwords from multiple PDF files using Applescript or by creating a Workflow in OS X?
My scenario is that I have multiple password protected PDF files in a folder. I know the passwords for all, which is same. I want to be able to run a Workflow on this folder so that all PDFs inside it are unlocked by the workflow.
OR run an Applescript shell code on all these files at once
I also preferably want to be able to create a way where putting / moving / pasting any PDF in the folder automatically unlocks it :)
Help appreciated !!
Update:
I have tried pdftk. The following code works awesome in Terminal, once pdftk is installed
pdftk secured.pdf input_pw foopass output unsecured.pdf
Now I want to be able to create a workflow that runs this command on selected files or on all the files in a folder
The AppleScript command to execute a shell script is do shell script...
So something like this:
do shell script "pdftk secured.pdf input_pw foopass output unsecured.pdf"
should work.
At this point I see 2 options:
write an AppleScript script that ask the user for the folder or get it from the Finder selection and then execute the command for each file in the folder;
write an Automator workflow that get the files from the folder using already available actions and then attach a new action that execute the AppleScript script.
For option 2 you can set an Automator workflow as in the following image.
Have you heard of "Folder Actions"? It's a way to attach an applescript to a folder so that whenever a new file is added to the folder the applescript is run. A quick google search turned up this which will give you directions on how to set it up. You can do more google searching if you still have questions.
Here's an applescript you can use with folder actions. I didn't test it but it should work (it's basic code). This will do its stuff on only pdf files. Other files you add to the folder will be left alone. NOTE: you have to put in your values for the first 4 variables of the script.
Good luck.
on adding folder items to theFolder after receiving theItems
-- enter your values here
set pdftkPosixPath to "/usr/bin/pdftk"
set pWord to "foopass"
set appendedName to "_unlocked" -- text to append to the file name
set shouldTrash to true -- true or false, move the locked file to the trash after unlocking?
set fContainer to theFolder as text
repeat with anItem in theItems
try
tell application "System Events"
set fName to name of anItem
set fExt to name extension of anItem
end tell
if fExt is "pdf" and fName does not contain appendedName then
set baseName to (text 1 thru -5 of fName) & appendedName & ".pdf"
set newPath to fContainer & baseName
do shell script (quoted form of pdftkPosixPath & space & quoted form of POSIX path of anItem & " input_pw " & quoted form of pWord & " output " & quoted form of POSIX path of newPath)
if shouldTrash then
tell application "Finder" to move anItem to trash
end if
end if
end try
end repeat
end adding folder items to
EDIT: here's how you can ask for a password. Note that if you want to see the text then remove "with hidden answer".
display dialog "Enter a password:" default answer "" with icon note with hidden answer
set theAnswer to text returned of the result
if theAnswer is not "" then set pWord to theAnswer

Resources