Run a Terminal command on open location AppleScript - macos

I want to run a command in terminal when a custom url is called.
Example :
I want to open a terminal and run echo "hello myapp://hello" when someone opens myapp://hello in browser or when some one executes open "myapp://hello"
I am new to apple script so I searched a lot but was unable to conclude with a working code snippet.
I tried :
on open location this_URL
display alert this_URL
tell application "Terminal"
reopen
do script "echo 'hello " & this_URL & "'"
activate
end tell
end open location
Updated the Info.plist as:
.....
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>myapp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
....
This code just opens a terminal but doesn't runs the echo command.
Standalone parts of code :
on open location this_URL
display alert this_URL
end open location
and
tell application "Terminal"
reopen
do script "echo 'hello " & this_URL & "'"
activate
end tell
Works perfectly.
But the combination doesn't work....
Please tell me some workaround.
Thanks in advance

I think this is problem with authorization.
I don't know why but when we add CFBundleURLTypes in plist then macOS prevents us to send events to terminal.
So here is a workaround :
Make 2 apps instead of one as follows
App 1 : url handler
So this will handle the incoming url copy it to clipboard and initiate the 2nd app.
Its applescript will be as follows :
on open location this_URL
set the clipboard to this_URL
tell application "/path/to/app2.app" to activate
end open location
Add CFBundleURLTypes to its plist
App 2 : Terminal handler
It will read the url from clipboard and then run it on terminal.
Its applescript will be as follows :
tell application "Terminal"
set this_URL to ( the clipboard as text )
do script "echo 'hello " & this_URL & "'"
activate
end tell
set the clipboard to ""

Related

How to rename files as they are added to a folder?

I created an Automator workflow that renames iOS Simulator Screenshots as they are added to the Desktop folder. But for the workflow to work, I need to run it myself manually. Is there a way to make the workflow watches the Desktop folder continuously and renames screenshots as they are added like a background service?
This is the workflow I created :
Screenshot of the Workflow
I find automator to be a a bit clunky at times, so I suggest you do this with a standard AppleScript Folder Action. Copy the following script into Script Editor.app, modify the prefixes as needed, then save it in ~/Library/Scripts/Folder Action Scripts.
property old_prefix : "Simulator Screen Shot - "
property new_prefix : "some text "
on adding folder items to this_folder after receiving these_items
repeat with this_item in these_items
set item_path to POSIX path of this_item
tell application "System Events"
set this_disk_item to disk item item_path
tell this_disk_item
if its name begins with old_prefix then
set its_extention to its name extension
set its_name to its displayed name
set new_file_name to new_prefix & (text ((length of old_prefix) + 1) through -1 of its_name)
if new_file_name ≠ its displayed name and new_file_name ≠ "" then
set name of this_disk_item to new_file_name & "." & its_extention
end if
end if
end tell
end tell
end repeat
end adding folder items to
Use Spotlight to open the app "Folder Actions Setup", add your desktop as a folder on the left side, and on the right, select and attach the script you just saved to that folder. It should 'just work'.
The way I would do it is to use an application called launchd to open the automator script how ever often you need (e.g. every few seconds). This website describes how to set it up. Basically you have to first save your automator script as an application into a certain folder (e.g. Documents), then write a short xml script which tells launchd which file to open and how often to do it.
Below is a sample script:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.NAME.test</string>
<key>Program</key>
<string>/Users/USERNAME/Documents/test.app/Contents/MacOS/Application Stub</string>
<key>StartInterval</key>
<integer>5</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Open a new "Plain text" TextEdit document and paste in the above. Replace the Label (com.NAME.test) with whatever you want; I usually just put my name in there, followed by the name of the script. Next change the line under Program to the location of your ".app" file you saved earlier, remembering to change USERNAME to your username. Keep in mind that the /Contents/MacOS/Application Stub needs to be right after the ".app" part so that the script will start your application. Then change the line under StartInterval to the number of seconds you want the script to wait before it runs again.
After you're done editing the script, save it to "/Users/USERNAME/Library/LaunchAgents/com.NAME.test.plist," of course changing USERNAME to your username and com.NAME.test to the Label used in the xml script. If it asks if you want to save it with the ".plist" extension, choose yes. Once the file's saved, open up Terminal (/Applications/Utilities/Terminal.app) and type in the command launchctl load /Users/USERNAME/Library/LaunchAgents/com.NAME.test.plist, changing the file name to the filepath of your ".plist" file. Use unload instead of load to stop the script from running.
For me a gear icon kept appearing in the menu bar every time the script ran, so I found on this and this website that you can stop it by adding "Run Shell Script" to the very top of your Automator script, then typing in the box killall ScriptMonitor || true.

Get iTerm location Applescript

I would like to know how to get the current location of the current session in iTerm using Applescript.
I have tried all sorts of things. The dictionary is very difficult for me to understand.
Here is my latest attempt
tell application "iTerm"
tell the current session of current terminal
set theSelection to attachment file name
display dialog theSelection
end tell
end tell
And the error this produces is:
error "iTerm got an error: Can’t get file name of current session of current terminal." number -1728 from file name of current session of current terminal
I just want the location the iTerm session is currently in: /Users/SuperCoolGuy/Desktop
This is ugly...
tell application "iTerm"
if not (exists terminal 1) then reopen
set mySession to current terminal's current session
tell mySession
write text "pwd"
set sessionContents to it's text
end tell
end tell
set myPath to paragraph -2 of (do shell script "grep . <<< " & quoted form of sessionContents)
If somebody stumbles upon this question, there's another option now with Shell integration. With it you can access the path in AppleScript using the iTerm defined variable session.path:
activate application "iTerm"
tell application "iTerm"
tell current session of current window
set myTerm to (variable named "session.path")
write text "echo this is the current path: " & myTerm
end tell
end tell
That information isn't currently available through iTerm's AppleScript interface. The only exposed properties of the session object having to do with what's running in it (as opposed to its visual appearance) are contents and tty, and those aren't what you're looking for here.

Applescript to open terminal, run command, and show - Not working

I´m trying to create a keyshortcut to open terminal in current folder. Looking around, I found this code to create a service (the part of adding the shortcut to this service is solved), only added things are the "; clear" and some of the "activate" so it shows
on run {input, parameters}
tell application "Finder"
activate
set myWin to window 1
set theWin to (quoted form of POSIX path of (target of myWin as alias))
tell application "Terminal"
activate
tell window 1
activate
do script "cd " & theWin & ";clear"
end tell
end tell
end tell
return input
end run
It is not working as i would like.
troubles:
it opens two windows in terminal, have no idea why. It has nothing to
do with the added "activate"… it has always donde that
if I select an item on finder ( a folder ) it opens its parent directory and i would
like it to open the selected folder
this is my very first try with Applescript so if the error is obvious i just can't see it
Thanks in advance
The do script command already opens a window in Terminal. Try it this way:
tell application "Finder" to set theSel to selection
tell application "Terminal"
set theFol to POSIX path of ((item 1 of theSel) as text)
if (count of windows) is not 0 then
do script "cd " & quoted form of theFol & ";clear" in window 1
else
do script "cd " & quoted form of theFol & ";clear"
end if
activate
end tell
I like the reopen approach better...
tell application "Finder" to set currentFolder to target of front Finder window as text
set theWin to currentFolder's POSIX path
tell application "Terminal"
if not (exists window 1) then reopen
activate
do script "cd " & quoted form of theWin & ";clear" in window 1
end tell

Run applescript in Automator

I need to start and stop a small server with automator but my knoledge is very limited. I can't manage to set the path where the file is and I don't know how to stop the server.
So far I have this:
on run
set r to display dialog "Start or stop the NINJAM server ?" buttons {"Stop", "Start"}
if button returned of r is "Start" then
#tell application "Terminal"
# activate
do shell script "cd \"/Applications/ MUSIC/ Utilities/Audio IP/NINJAM/NINJAM/NinjamOSXServer ./ninjamsrv Server.cfg\""
#end tell
else
do shell script "Stop"
end if
end run
Any help is really apprectated. Thanks in advance.
NOTE that I'm using my own path here -- I put the ninjam server folder in the top level of my Applications folder.
I had to create a 'term' file, which is a text file with this in it:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>WindowSettings</key>
<array>
<dict>
<key>ExecutionString</key>
<string>cd /Applications/NinjamOSXServer/;./ninjamsrv config.cfg</string>
</dict>
</array>
</dict>
</plist>
I saved this as LaunchNinjamSrvr.term and put it in the same folder as the ninjamsrvr. Then the script to use is:
set r to display dialog "Start or stop the NINJAM server ?" buttons {"Stop", "Start"}
if button returned of r is "Start" then
do shell script "open /Applications/NinjamOSXServer/LaunchNinjamSrvr.term"
else
do shell script "killall -INT -v ninjamsrv"
end if
[Occurs to me that I should give some explanation. Directly using the full path with the " config.cfg" parameter makes 'do shell script' choke. Splitting into two commands (but still using do shell script), like you see in the .term file, works to launch ninjamsrv, but makes the script editor (I use Smile) freeze. So that is (presumably -- I didn't want to test it by other means [script app, etc.]) a problem, and why I resorted to using the .term file. It used to be that you could, from the File menu in Terminal (as I recall), save a .term file directly, but that seems to have fallen by the way-side. So, at this point, I have a template that I use and just paste commands into the appropriate line. (But see http://discussions.apple.com/thread/3139585?start=0&tstart=0 -- wherein the technique of exporting Terminal Preference file is explained). I'm being a bit lazy in that the new form is .terminal, not .term ... anyway ...
So now all that is left is doing the actual AS script. 'open' is a basic command line command which is just like opening or double-clicking in the Finder. If, for some reason your file opens in the wrong app or doesn't open, you might need to map it to Terminal.app (in the get info window) and/or change the extension to the more up--to-date '.terminal'.
killall is like kill, designed to kill processes in various ways. I chose -INT because this is essentially like doing a control-c to interrupt the process.]

Applescript to open .url files on OS X Firefox

For reasons that only the developers can understand, Firefox will create and open .url files on Windows and .webloc files on OS X but won't allow the Windows version of Firefox to open .webloc files or the OS X version of Firefox to open .url files. (.url files open in Safari but that's not good enough for reasons that aren't worth going into here.) As part of my efforts to use either filetype on either system, I'm writing an applescript to open .url files on OS X Firefox.
on open the_droppings
set filePath to the_droppings
set fp to open for access filePath
set fileContents to read fp
close access fp
set secondLine to paragraph 2 of fileContents
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "="
set URLstring to last text item of secondLine
set AppleScript's text item delimiters to tid
tell application "Firefox"
activate
OpenURL URLstring
end tell
end open
I thought this would work but in the 3rd to last line it says "Expected end of line, etc. but found identifier." Why is this?
EDIT sakra's answer below mostly works but breaks on urls containing "=" such as: http://example.com?foo=a&bar=z
Firefox does not seem to feature an AppleScript dictionary at all. Therefore the term OpenURL within the tell app "Firefox" statement is interpreted as an AppleScript identifier and not as an AppleScript command. Two AppleScript identifiers in a row result in a syntax error.
As a work-around you can use the shell command open in combination with the standard AppleScript command do shell script:
on open the_droppings
set filePath to the_droppings
set fileContents to read filePath
set theOffset to offset of "URL=" in fileContents
set URLstring to text (theOffset + 4) through -1 of fileContents
do shell script "/usr/bin/open -a Firefox.app " & quoted form of URLstring
end open
EDIT: download my script in an app here: http://www.mediafire.com/?v77bv9gl9e7oj40
This works even better:
on open the_droppings
set filePath to the_droppings
set fileContents to read filePath
set theOffsetA to offset of "URL=" in fileContents
set theOffsetB to offset of "IDList=" in fileContents
set URLstring to text (theOffsetA + 4) through (theOffsetB - 3) of fileContents
do shell script "/usr/bin/open -a Firefox.app " & quoted form of URLstring
end open
It reads the URL= line to the next line ( IDList ) minus 3 steps ( to ignore the \r\n ) and sends it to firefox. Works like a charm for me.
I however have seen url files with weird layout ( example: http://forums.mozillazine.org/viewtopic.php?p=2619487 ), which I'm not sure of if it would work there. But I checked a lot of my url files and they dont have that, so atleast it works fine for me. Let me know if you run into troubles with this script. For the moment I'm setting it as default app to open URL files, instead of Safari!
To be able of opening url files with an applescript file, it needs to have the proper doctypes and identifier set in its plist, like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleAllowMixedLocalizations</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>url</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>document.icns</string>
<key>CFBundleTypeName</key>
<string>URL File</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>URL</string>
</array>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>droplet</string>
<key>CFBundleIdentifier</key>
<string>filehandler.url.mozilla.firefox</string>
<key>CFBundleIconFile</key>
<string>droplet</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>FirefoxURLHandler</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>dplt</string>
<key>LSMinimumSystemVersionByArchitecture</key>
<dict>
<key>x86_64</key>
<string>10.6</string>
</dict>
</dict>
</plist>
I know it's not apple script, but I did this to open them on Linux, but it has the problem that if and when the terminal closes, it kills the browser. I opened another question about this, but this may help.
#!/bin/bash
#bash -c "cat $1 | grep URL | cut -d'=' -f2 | xargs firefox &"
echo OpenWinURL in firefox
echo Closing this window will close your firefox.
echo Try opening firefox before opening a OpenWinURL
#echo Copyright 2021 Aaron Peterson GPL V2 or later
#echo The ampersand doesn't work after firefox
#echo "$1"
#
jobs -l
cat "$1" | grep -m 1 URL= | cut -d'=' -f2- | xargs -0 -i firefox {} #&
jobs -l
disown -h -a
jobs -l
#read -n 1 -p "Input Selection:" mainmenuinput
#echo testing
#wait 1000
#
#| head -n 1
#| cut -d'=' -f2 | xargs firefox &

Resources