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?
Related
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.
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
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.
I need to do a .command file script/batch. Launching it (double-click) it has to to those things:
Open a terminal window (A)
Launching a command that open the folder where the file is (maybe this cd "dirname "$0"")
Launch a command
Open a terminal window (B)
Launching same command at point 2
Launch a command
Given that you explicitly want to create terminal windows, consider creating an application using AppleScript:
Open Script Editor (up to 10.9, AppleScript Editor)
Paste the code below.
Save as an application (via the pop-up list in the Save As dialog) to the desired folder.
# Determine the folder in which this app is located.
set thisFolder to do shell script "dirname " & quoted form of POSIX path of (path to me)
# Sample commands to execute in the new windows.
set cmds to {"date", "echo $$"}
tell application "Terminal"
# Create 2 new windows, change to the
# this app's folder, and execute the respective command.
repeat with i from 1 to 2
do script "cd " & quoted form of thisFolder & "; " & item i of cmds
end repeat
# Activate Terminal.app
activate
end tell
The reason that I recommend using an application over a *.command file is that the latter would itself open in a Terminal window first, before creating the desired windows, which is visually disruptive (and, depending on your Terminal.app preferences, may leave the extra window open).
Alternatively, you could turn that into a virtue and use the *.command file's own window as your 1st terminal window, and only create one additional one.
In Script editor i have written a command to boot the JAR.
do shell script "cd /Desktop/RunJar/; java -jar RunMyJar.jar"
and Saved as script file as a Application. When i click on script file jar get run.
My requirement
I would like get the name of file that has been dropped onto the script file and would like to pass the name of that dropped file as an argument to my jar.
I have implemented this in Windows but could not working similar on MAC O.S.
On Windows
I have placed a BAT file to Boot JAR along with the absolute file name, that has been dropped on the bat file on windows. #echo %* will give me the list of files that has been dropped onto Batch file.
#echo %*
#pause
java -jar RunMyJar.jar %*
Similar i would like to implement in MAC O.S.
Thanks.
I found the following example at: Ben's AppleScript Snippets:
on open of finderObjects -- "open" handler triggered by drag'n'drop launches
repeat with i in (finderObjects) -- in case multiple objects dropped on applet
displayName(i) -- show file/folder's info
if folder of (info for i) is true then -- process folder's contents too
tell application "Finder" to set temp to (entire contents of i)
repeat with j in (temp)
display dialog j as string -- example of doing something with each item
end repeat
end if
end repeat
end open
You can also easily modify my answer to a similar question:
on open of theFiles -- Executed when files are dropped on the script
set fileCount to (get count of items in theFiles)
repeat with thisFile from 1 to fileCount
set theFile to item thisFile of theFiles
set theFileAlias to theFile as alias
tell application "Finder"
set fileInfo to info for theFileAlias
set fileName to name of fileInfo
-- something to this effect, but now that you have the file name,
-- do what you will...
do shell script "cd /Desktop/RunJar/; java -jar " & fileName
end tell
end repeat
end open
To add to Paul's answer
on open of finderObjects
repeat with currFile in finderObjects
-- ok, we have our current item. But it's an "alias": a Mac OS path
-- not a POSIX path
set unixPath to POSIX path of currFile
set base to do shell script "dirname " & unixPat
set fname to do shell script "basename " & unixPath
-- you could ask the Finder to give this to you too -- I'll use this way because
-- I'm guessing it's more familiar to you
do shell script "cd '" & base & "';" & " java -jar " & fname
end repeat
end open