Then I had the idea to do the same in AppleScript. The script below works for simple scripts like getting the mailboxes for Mail. But if I use the script on itself I get an empty script as result:
use framework "Foundation"
use scripting additions
tell application "Finder"
set theFile to choose file with prompt "Please select a text file:" of type {"public.text"}
set theFile to theFile as string
set FileContent to read file theFile as string
set theScript to "applescript://com.apple.scripteditor?action=new&script=" & my urlEncode(FileContent)
open location theScript
end tell
on urlEncode(input)
tell current application's NSString to set rawUrl to stringWithString_(input)
set theEncodedURL to rawUrl's stringByAddingPercentEscapesUsingEncoding:4 -- 4 is NSUTF8StringEncoding
return theEncodedURL as Unicode text
end urlEncode
I think, you should remove telling to Finder first of all.
Also, file specification has problem when using with AsObjC frameworks. Use alias reference instead:
use framework "Foundation"
use scripting additions
set theAlias to choose file with prompt "Please select a text file:" of type {"public.text"}
set FileContent to read theAlias
set theScript to "applescript://com.apple.scripteditor?action=new&script=" & my urlEncode(FileContent)
open location theScript
on urlEncode(input)
tell current application's NSString to set rawUrl to stringWithString_(input)
set theEncodedURL to rawUrl's stringByAddingPercentEscapesUsingEncoding:4 -- 4 is NSUTF8StringEncoding
return theEncodedURL as Unicode text
end urlEncode
The stringByAddingPercentEscapesUsingEncoding: method is deprecated after Mac OS X 10.11. The method to use now is stringByAddingPercentEncodingWithAllowedCharacters: with an NSCharacterSet parameter. I haven’t had any luck with any of the obvious URL…AllowedCharacterSet presets, but allowing just alphanumeric characters seems to work — with this script on itself at least. I haven’t tested it with other code. An “allowed character”, by the way, is one that doesn’t need to be percent encoded.
use framework "Foundation"
use scripting additions
set theAlias to choose file with prompt "Please select a text file:" of type {"public.text"}
set FileContent to read theAlias
set theScript to "applescript://com.apple.scripteditor?action=new&script=" & my urlEncode(FileContent)
open location theScript
on urlEncode(input)
set rawURL to current application's NSString's stringWithString:(input)
set allowedCharacters to current application's NSCharacterSet's alphanumericCharacterSet()
set theEncodedURL to rawURL's stringByAddingPercentEncodingWithAllowedCharacters:(allowedCharacters)
return theEncodedURL as Unicode text
end urlEncode
Hi,
wondering what wud be the code to open url in terminal.app, instead of script editor.
This wud be great to run scripts from markdown links (eg from obsidian)
Tried “osascript://com.apple.Terminal?action=new&script” did not work
This works “ssh://192.168.1.1”
Suggestions plz
Cheers
Hi bwill
I do remember coming across one. I was naïve about these URL then. I have used applescript URL for along time.
Popclip has a function to convert selected text into various formats camelcase/ plain text etc. I modified it to generate markdown url and paste it into obsidian
It is much easier to tag and search and retrieve scripts when needed in obsidian, and it is as easy as clicking link → it opens in script editor
If you create a markdown link with “ssh://One208@192.168.1.1”, and click it, it opens a terminal window asking password for user One208
I am sure url for terminal exists or may be not
Cheers