Automator / Applescript : how to get original from folder alias - applescript

I'm trying to create a context menu shortcut to open a file/folder in VS Code from the original item or its alias
So far I was able to create an Automator Service, which:
receives selected: files or folders
in: any application run
shell script:
open -n -b "com.microsoft.VSCode" --args "$*"
How can I change it to accept also aliases?

Symbolic links should be OK, but Finder aliases usually don't work, since most shell utilities see them as small data files and don't know how to interpret them. One solution would be to add a Run AppleScript action to look for aliases in the input and use the original item instead, for example:
Service receives selected files or folders in any application
Run AppleScript:
on run {input, parameters}
set output to {} -- this will be a list of the output items
tell application "Finder" to repeat with anItem in the input
if anItem's kind is "Alias" then
set the end of output to POSIX path of (original item of anItem as alias)
else
set the end of output to POSIX path of anItem
end if
end repeat
return output
end run
Run Shell Script, etc

Related

How to open an EMACS file in OS X by double-clicking on it, using the `emacsclient` command?

I want to open an org-mode file selected in the Finder, by double clicking on it. But since I use Emacs in daemon-mode, I want to use the emacsclient command for that.
Thus the primary idea was to wrap the command emacsclient -c posixPathToFile in an AppleScript App to open it.
tell application "Finder"
set fileAlias to the selection as alias
set fileName to name of fileAlias
set posixPath to POSIX path of fileAlias
end tell
-- tell application "Emacs" to activate
try
do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
end try
I know some set commands are not needed. Let's assume this script is saved as Xemacs.app and that I associate this app to always open .org file.
Using this App does not work by double-clicking on the file, but rather if I select the file in the Finder and then call the Xemacs.app independently. Why ? I'm not confident enough with AppleScript to figure out what happens.
So the workaround was to use the Automator service
on run {input, parameters}
set posixPath to POSIX path of input
tell application "iTerm" to do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
return input
end run
The service is saved as 'Open in Emacs'
Now selecting a file and right-clicking and callig Service > "Open in Emacs" works and opens the file.
What is wrong with the first approach ?
ok, I solved my issue. The problem comes from my misunderstanding of the difference between ScriptEditor and the Automator. If I use the Automator to create an App and use the former script instead of creating an App using the ScriptEditor, then it works as expected.
One can simplify the process by creating an App in Automator and running a shell script instead of wrapping the command in Ascript.

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 copy files based on partial file name

Forgive me, I'm a bit of an amature.
I'm working to comply with a records request for hundreds of students. All files are named with the first 5 digits of the name being the student's ID number. I created the script below and it runs but with no results.
I would welcome any help that you guys can provide.
with timeout of 3600 seconds
tell application "Finder"
set myFiles to files of folder POSIX file "/Volumes/Storage/Records" as alias list
end tell
repeat with aFile in myFiles
tell application "System Events"
set myvalues to {"11111", "22222", "33333", "44444", "55555", "66666", "77777", "88888", "99999", "00000", "11112", "22223", "33334", "44445", "55556", "66667", "77778", "88889", "99990"}
if name of aFile contains myvalues then
copy aFile to folder POSIX file "/Volumes/Storage/Records"
end if
end tell
end repeat
end timeout
First of all, the command to copy files in terminology of Finder and System Events is duplicate.
Second of all (and the main issue) you have to check the first 5 characters not the entire name.
And third of all, it’s not quite useful to copy the files to the same place but I guess it’s only a placeholder
property IDNumbers : {"11111", "22222", "33333", "44444", "55555", "66666", "77777", "88888", "99999", "00000", "11112", "22223", "33334", "44445", "55556", "66667", "77778", "88889", "99990"}
with timeout of 3600 seconds
tell application "Finder"
set myFiles to files of folder "Storage:Records:"
repeat with aFile in myFiles
set IDPrefix to text 1 thru 5 of (get name of aFile)
if IDPrefix is in IDNumbers then
duplicate aFile to folder "Storage:Records:Destination:"
end if
end repeat
end tell
end timeout
My solution uses only the Finder because it doesn't include the invisible files and it's not needed to coerce the Finder object specifiers to alias.
If the location of the files is on an external volume, it’s easier to use the HFS path “Storage:Records” than the coercion from POSIX path POSIX file "/Volumes/Storage/Records” as alias
I suggest you to use do shell script function instead of using Finder application as it's more flexible solution. And you can find any errors in your command easily because you can debug it using the Terminal.
do shell script "<your commands here>"
For your use case you need to use a couple of commands inside do shell script: find and then cp.
I would suggest a simple bash script is a better option here. Save the following script on your Desktop as CopyFiles
#!/bin/bash
# Make a subdirectory to copy the results to
mkdir results 2>/dev/null
# Read all ids from file "ids.txt"
while read id; do
echo Processing id: $id
# Remove the word "echo" on following line to actually copy files
echo cp /Volumes/Storage/Records/${id}* results
done < ids.txt
It assumes there is a file on your Desktop called ids.txt that looks like this
11111
22222
12345
54321
Then start a Terminal, by pressing Cmd+Spacebar and typing "Terminal" and hitting "Enter".
Go to your Desktop and make the script executable with
cd Desktop
chmod +x CopyFiles
and then run it with
./CopyFiles
At the moment, it does nothing except tell you what it would do like this:
Processing id: 11111
cp /Volumes/Storage/Records/11111* results
Processing id: 22222
cp /Volumes/Storage/Records/22222* results
Processing id: 12345
cp /Volumes/Storage/Records/12345* results
Processing id: 54321
cp /Volumes/Storage/Records/54321* results
If it looks like it is doing what you want, edit the script and remove the word echo where noted and run it again.

Run several Applescripts from within current script

I'd like to expand my existing...
run script file "Macintosh HD:Users:pathTo:myScript.scpt"
to run all scripts found in a given directory. I've tried...
tell application "Finder" to set scriptsToRun to files of folder POSIX file "/Users/pathTo/" as alias list
run script file scriptsToRun
but no luck with that. Also unless necessary I don't particularly need to involve Finder in my equation. Any suggestions appreciated.
scriptsToRun is a list, so you need to repeat over the list and run each one separately. Notice I used parenthesis to ensure the code is interpreted correctly in the Finder line.
Also notice you don't need "file" in the "run script" line because the list of files is already a list of alias files... from the Finder line. You would only need the word "file" if you had a list of files in string format, then you'd use "file" before each string to make it a file specifier before running it.
Good luck.
tell application "Finder" to set scriptsToRun to (files of folder POSIX file "/Users/pathTo/") as alias list
repeat with aScript in scriptsToRun
run script aScript
end repeat

AppleScript choose file or folder

can I use AppleScript to choose either file or folder in one time?
Now I could use
tell application "SystemUIServer" to return POSIX path of (choose file)
or
tell application "SystemUIServer" to return POSIX path of (choose folder)
to get file or folder. However I cannot get file or folder in one time.
No, you can't do it with "choose file" or "choose folder" verbs, but choosing a file or folder (or multiple files/folders) is supported by the underlying NSOpenPanel. So you can do it with AppleScriptObjC. Here's an example using ASObjCRunner (derived from here):
script chooseFilesOrFolders
tell current application's NSOpenPanel's openPanel()
setTitle_("Choose Files or Folders") -- window title, default is "Open"
setPrompt_("Choose") -- button name, default is "Open"
setCanChooseFiles_(true)
setCanChooseDirectories_(true)
setAllowsMultipleSelection_(true) -- remove if you only want a single file/folder
get its runModal() as integer -- show the panel
if result is current application's NSFileHandlingPanelCancelButton then error number -128 -- cancelled
return URLs() as list
end tell
end script
tell application "ASObjC Runner"
activate
run the script {chooseFilesOrFolders} with response
end tell
ASObjCRunner converts a NSArray of NSURL objects into an AppleScript list of files; the results can look something like:
{file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}
Firstly, you don't need a tell for that.
POSIX path of (choose file)
Secondly, it is not clear why you need this. Do you mean you want to select a file and it's folder? That's not how you do it; you select the file then parse the file path for the containing folder or use one of the many methods to do that, like
set f to (choose file)
set posixF to POSIX path of f
tell application "Finder" to set filesDir to container of f as alias as text
set posixDir to POSIX path of filesDir
{f, posixF, filesDir, posixDir}
If you want to be able to select multiple folders and files at the same time, I don't think there is a "pure applescript" way to do this (aside from using a drag-drop aware script application).

Resources