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.
Related
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.
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.
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.
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?