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.
Related
There are many posts that explain how to drag-and-drop things into an open Terminal window. But what I would like to do is to tell Terminal to drag-and-drop a previously selected directory onto another application like VSCode or Renamer. I have not found any documentation for that. Is it at all possible? And if so, would somebody please point me to a documentation?
UPDATE:
I'd like to clarify my question with what I intend to do:
Pre requisites:
a "work folder" contains folders and files that shall be renamed
the renaming is done by an application called "A better finder renamer" (which allows presets)
An "Automator" (MacOS app) action shall imitate these steps:
the "work folder" is right clicked
the folder is drag-and-dropped onto the ABFR, which initiates the currently active preset
other actions via bash (like 'mv .//.* ./') ...
It is the "drag-and-drop" part of the Automator action that presents a riddle for me.
The "drag-and-drop" operation is manual operation. In AppleScript, instead the command to open the file or folder is given to the destination application itself.
Second thing to keep in mind. Getting Terminal's current working directory is easy with its do script "pwd" command. But the result of the do script Terminal command returned to the script is always the window tab, not the result of the pwd shell command. One solution is to redirect (remember) the result of pwd in a temporary text file.
set tempFolder to (path to temporary items folder from user domain)
set temp to POSIX path of tempFolder & "workingDirectory.txt"
tell application "Terminal" to do script ("pwd > " & temp) in selected tab of window 1
set curDirPosixPath to paragraph 1 of (read file ((tempFolder as text) & "workingDirectory.txt"))
set curDirHFSPath to curDirPosixPath as POSIX file as Unicode text
tell application "Visual Studio Code" to open curDirHFSPath
.
NOTE: other possible solution (I don't like) is parsing the text contents of Terminal window after pwd command execution. You can get contents using property contents (of selected tab of front window).
Open Automator, choose create New Document.
Choose create new Quick Action (service).
Set workflow receives current folders in any application.
From library Add Run AppleScript action. Edit it contents:
on run {input, parameters}
set curDirHFSPath to (item 1 of input) as text
tell application "Visual Studio Code" to open curDirHFSPath
end run
Save this Quick Action as service. Now when right-clicking the folder, opens the context menu, where you can choose and run this service.
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 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.
I want to be able to double click to open a files type (HTML) in a particular app (SeaMonkey composer).
Double clicking the file opens SeaMonkey Browser, but I want it to open in Seamonkey Composer. The only way to do it is with the following command line
seamonkey -editor "filename.html"
So, how can I use apple script or automator to open my html files in composer ?
Save the following script as Application in Script Editor:
on run filesList
repeat with fileRef in filesList
do shell script "seamonkey -editor " & quoted form of POSIX path of fileRef
end repeat
end run
Select View > Show Bundle Contents and give it a custom bundle ID. You can then change the file association as above.
The above assumes the seamonkey command is itself just a launcher; if it's actually the full application (which may be the case as it's obviously not a native Mac app), the middle line'll need tweaked a bit:
do shell script "nohup seamonkey -editor " & quoted form of POSIX path of fileRef & " >/dev/null 2>&1"
That should allow the shell script to exit as soon as the seamonkey process is launched, leaving Seamonkey running until you quit it from its GUI.
If you want to use AppleScript to do this, this simple script should do the job:
set filePath to ((path to documents folder) as text) & "filename.html"
tell application "Seamonkey Composer" to open filePath
I don't have the Seamonkey Composer app to test, but it works with BBEdit.
Note that the open command must have a full path to the file.
Just 3 lines of script to be able to test a droplet application without leaving applescript editor
set fich to POSIX file "/Appli/conv2spct.app" as string
tell application "Finder" to open POSIX file "/Users/yourusername/Desktop/somefile" using application file fich
If there are errors in your droplet a display dialog will be opened by the script editor applescript
The same script with choose file for the 2 elements
set fileappli to POSIX path choose file of type {"APPL"} with prompt "Choose a Droplet application to debug"--the droplet for debug
set fileargument to POSIX path choose file --the file argument to pass at droplet
tell application "Finder" to open fileargument using application file fileappli
If there are errors in your droplet a display dialog will be opened by the script editor applescript
Here's a pragmatic alternative using do shell script, which potentially allows you to specify multiple file arguments:
do shell script "open -a /Appli/conv2spct.app ~/Desktop/somefile1 ~/Desktop/somefile2"
The above paths happen not to need quoting (escaping) for the shell, but when using variables to specify the file paths, it's best to use quoted form of (to pass multiple arguments, apply quoted form of to each):
do shell script "open -a " & quoted form of fileappli & " " & quoted form of fileargument
Looks like this has become simpler since the question was first asked. According to this documentation you can write:
open {choose file}
on open theDroppedItems
...
end open
Run this from within the AppleScript editor and the file you choose will be opened as if it had been dropped onto the compiled script.