mac automator multiple instances of app with selected file - macos

I am writing an automator script that should open a new instance of the app fslview everytime I double click on a selected nii.gz file. All attempts to "run shell script" using the & operator, to make fslview run in the background, failed. I managed to use "run applescript" with
do shell script "open -n /usr/local/fsl/bin/fslview"
to open multiple instances of fslview. How do I pass the path and name of the selected file from "get selected finder items" to fslview? The terminal command I use for my desired behavior looks like this:
fslview some_image.nii.gz &
Thanks in advance,
Martin
BTW: when I run the following script I get an infinte loop of opening fslview apps
on run {input, parameters}
set f to (input as text)
set f to POSIX path of f
do shell script "open -n /usr/local/fsl/bin/fslview.app " & f
end run

I found a solution:
on run {input, parameters}
set f to POSIX path of (input as text)
do shell script "source ~/.bash_profile"
do shell script "open -n -a fslview.app --args " & f
end run

Related

Writing two commands into terminal using AppleScript

With an AppleScript I am trying to open a terminal window and set the terminal to cd into certain folders. I want the terminal to cd into multiple folders. but it keeps opening up 2 windows and doing the 2 commands in 2 separate windows.
set desktop_folder to "$HOME/Desktop"
tell application "Terminal"
do script "cd desktop"
do script "cd myfolder"
end tell
how can i set it so that the terminal will execute these commands in the same window?
Every do script command of Terminal.app opens new window. So, to send multiple commands to the same window, you should use only one do script command. Like here:
set desktop_folder to "$HOME/Desktop"
set myfolder to quoted form of (POSIX path of (choose folder))
tell application "Terminal" to do script "cd " & desktop_folder & ";cd " & myfolder
Note: this approach has little advantage - it successfully creates window 1 when no windows exists already.

Run Bash script on selected files in Finder

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.

AppleScript access not allowed when trying to run shell script

New to AppleScript but I'm trying to migrate over from terminal scripting. After much research I am having issues trying to get a shell script running from within the .app file.
What I have so far:
to the_foo()
tell application "Finder"
set current_path to container of (path to me) as alias
set path_in_posix to POSIX path of current_path
end tell
tell application "Terminal"
set new_terminal to do script " "
activate
do script "cd " & path_in_posix in window 1
do shell script "ls " & POSIX path of (path to me) & "Contents/Resources/Scripts/foobar.sh" in window 1
end tell
end the_foo
The error I am getting:
Learned to open a new terminal with: Applescript to open a NEW terminal window in current space
I added in window 1 when I learned that do script opens a new terminal window every time, referenced: applescript and terminal ( run several do shell script in one terminal window )
I originally tried:
set script_Location to path to resource "Contents:Resources:Scripts:"
set run_Script to (quoted form of script_Location) & "foobar.sh"
do shell script run_Script
after referencing: How to change AppleScript path to a Terminal-style path? but when I run it I get the same error.
So how can I run the shell script located within the Scripts folder within the same window 1? I would ideally like to set a variable for the path so I can put multiple shells scripts in the Scripts folder.
It's probably just a typo
do script "ls " & POSIX path of (path to me) & "Contents/Resources/Scripts/foobar.sh" in window 1
rather than
do shell script "ls " & ...
I recommend to use System Events instead of the Finder to get the container of the script
tell application "System Events"
set path_in_posix to POSIX path of container of (path to me)
end tell
Vadian had the correct and better approach in a one liner. I did change
"Contents/Resources/Scripts/foobar.sh"
to
set script_Location to "Contents/Resources/Scripts/"
set foobar to do script "bash " & POSIX path of (path to me) & script_Location & "foobar.sh" in window 1
this approach helps if I want to add more than one shell script in the Scripts folder.

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"

Open a file directly to emacs (by double-clicking)

How would I go about setting all .rb files or all .py files to open in emacs if I double-click them; say, from the desktop?
I'm on OS X 10.6.2.
I have the script:
on open of finderObjects
repeat with currFile in finderObjects
set unixPath to POSIX path of currFile
set base to do shell script "dirname " & unixPath
set fname to do shell script "basename " & unixPath
do shell script "cd '" & base & "';" & " emacs " & fname
end repeat
end open
But if I drag a file (text.py - just prints a line) on it, I get:
emacs: standard input is not a tty
And I'm unsure of how to solve this.
Thanks!
EDIT: Solved with link: http://www.macosxhints.com/article.php?story=20031027142625782
set filecount to 0
on open filelist
repeat with i in filelist
set filecount to 1
tell application "Terminal"
set filename to do shell script ¬
"perl -e \"print quotemeta ('" & POSIX path of i & "');\""
do script "emacs " & filename & "; exit"
end tell
end repeat
end open
if filecount < 1 then
tell application "Terminal"
do script "emacs; exit"
end tell
end if
For each type, select a file in the Finder, choose Get Info from the File menu. In the Info window that opens, under the Open with section, choose the emacs app you are using (and it must be an app version), and then press Change All.. .
If you are using the traditional curses emacs from the command line, you could build a small AppleScript app in the ScriptEditor or Automator action that would receive the files from the finder and open emacs with them. Then use Get Info to associate the app with .rb and .py files.
EDIT: See this recent answer for one way to create an AppleScript launcher app: just modify the shell command to call emacs instead.
FURTHER EDIT: Your script is almost there but you need to run the script under Terminal.app. Try modifying it like so:
launch application "Terminal"
tell application "Terminal"
do script "cd '" & base & "';" & " emacs " & fname
end tell
I'm not an OS X expert, but if emacs there is like it is on Unix and Windows it should have an emacsclient utility for feeding new files into an already running emacs session.
This previous SO question has some details on how to do this on a multitty setup. Perhaps some of that is applicable?

Resources