Run Bash script on selected files in Finder - bash

I have a tiny Bash script that executes ffmpeg and a touch command on an input file. I use this to recompress video files from my camera. I would like to be able to right-click files in Finder and run the script on the select file(s), preferably showing the terminal window while executing and closing when done.
How to do this on macOS?

I think this is what you want. I started Automator by pressing ⌘space and starting to type "Automator", hitting ↩ as soon as it guessed correctly. I then created a "Quick Action" that contains this code:
on run {input, parameters}
repeat with theItem in input
set f to POSIX path of theItem
tell application "Terminal"
activate
tell window 1
do script "echo " & f
end tell
end tell
end repeat
end run
and looks like this:
It basically just echos the filename, but you can put ffmpeg commands in there instead.

Why using finder? Or automator? Or going though loops and hoops just to use the GUI?
You have fully-functional bash shell in MacOS, so save time and hassle with the below one-liner.
Assuming you need to run your script for all *.mpeg files in the folder.
Try this:
ls *mpeg | xargs <your_script_name>
You will see the execution output in the same terminal window.

Related

Automator / Applescript : how to get original from folder alias

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

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.

Make an utility with automator which opens a specific folder with terminal and runs the 'compass watch' script

I have this automator setup which allows you to open a folder with terminal. The thing I want is that after the folder is opened via terminal, it should run the compass watch script.
This is what I currently have but I don't seem to find the solution myself. I tried adding some applescript and some bash, but nothing seems to work. Anyone know the solution?
Try this :
Add the "Run AppleScript" action in your workflow
If the compass watch script is a file, use this AppleScript script
on run {input, parameters}
set compasswatchScript to quoted form of "" -- drag drop the script file between double quote
tell application "Terminal"
do script compasswatchScript in window 1
end tell
end run
Insert the path of your script file in the second line of this script
--
if compass watch is a command, use this AppleScript script
on run {input, parameters}
tell application "Terminal"
do script "compass watch" in window 1
end tell
end run

Create a launcher for a node.js script

I'm trying to create a launcher for node.js scripts (so that I can run the scripts by clicking on their file icons instead of launching them from the terminal.) How is this usually done? I'd prefer if I could simply run a script in the terminal by clicking on its icon.
I tried writing a shell script to launch another script in the same folder, but it doesn't show the node.js script's command line output for some reason:
#!/bin/bash
echo -n "Enter a node.js script to run > "
read text
node "$text"
I now know that you're looking for an Ubuntu solution, but in case someone is interested in an OS X solution, here goes:
Open Automator.
Create a new application.
Add an AppleScript action
Paste the following code:
on run {input, parameters}
tell application "Terminal"
repeat with f in input
do script "node " & quoted form of (POSIX path of f)
end repeat
activate
end tell
end run
Save the application.
In Finder, control-click any *.js file and select Open With > Other ..., pick the new application and check 'Always Open With.'
From then on, whenever you open a *.js file, it will open in a new Terminal window that will stay open after node finishes running; add ; exit to the command string above to close automatically (possibly adding read -sn 1 first to wait for a keystroke first.)
i use this to start my node scripts on debian in the terminal
#!/usr/bin/env sh
dir=$(dirname $0)
script="$dir/path_to_your_server.js"
echo "node $script"

How can I convert bash's command substitution and pipe to an Applescript?

I need help converting this simple shell script to an apple script.
The point being because it is to be used in an Automator workflow, and so I need the Terminal window to be open, which cannot be done using a shell script.
The shell script is as follows:
java -classpath `dirname "$1"` `basename "$1" | sed "s/.class//g"`
This gets the location of the file, and then the name of the file, and then strips away the file extension of ".class", and then runs it using the Java command. So for example it would generate the following command:
java -classpath /users/desktop/ filename
I need to convert this command so that it works with Applescript so that I can then see the application run in the Terminal window. It would start like the following:
on run {input, parameters}
tell application "Terminal"
activate
do shell script "java -classpath path/to/ file"
end tell
end run
How can I port the text transformation to Applescript?
The only issue I'm seeing (right now) is to change do shell script to do script. Other than that, you've started it correctly. I'm assuming you want to pass (a) file reference(s) to the shell script. It's fairly simple...
set these_files to (choose file with multiple selections allowed)
repeat with this_file in these_files
tell application "Finder" to if the name extension of this_file is "class" then my do_shell_script(this_file)
end repeat
on do_shell_script(this_file)
tell application "Terminal" to activate --makes 'Terminal' the frontmost application
--'do shell script ...' goes here
--To refer to a file/folder for a 'do shell script', do something like the command below...
--do shell script "mdls -name kMDItemLastUsedDate " & quoted form of the POSIX path of this_file
end do_shell_script
I don't know AppleScript at all, but I suppose you could simply call your existing shell script in the do shell script line, instead of trying to redo the string manipulation in AppleScript.
Note:
It looks like you want to be able to invoke Java classes by click (or drag-n-drop or such). Your method will only work for classes in the anonymous package, i.e. without any package ...; declaration in the beginning of the source code. For others, you might try to find out in which package they are. I have no idea how to do this. Distributable programs should be in jar archives, anyway, where you should be able to start it with java -jar ....)

Resources