AppleScripts for use with OS X / macOS (tested in 10.9 Mavericks)
AppleScript applet for Spotlight URI handler →
This script, after you save it as an application, creates a URI scheme ("spotlight:") that can be used to do a Spotlight search in the Finder from a URL (link) in any macOS application. Further instructions are in the script. Read the comments in the script before running it.
AppleScript droplet for pandoc file conversion →
This script provides a graphical front end for Pandoc. It is an especially easy way to convert to multiple file formats at once, since output formats are chosen from a list. Just save it as an AppleScript droplet, and then drop files onto it that you want to convert. Read the comments in the script before running it.
AppleScript droplet to copy rich-text hyperlinks to clipboard →
This script copies rich text to the clipboard containing a (list of) hyperlink(s) to any file(s) that you drop onto the droplet, with the file URL as the destination of the link, with the filename as the link text, and with the creation date of the file in parentheses following the link. Just save it as an AppleScript droplet, and then drop a file or files onto it that you want to link. Then you can paste the result into any rich text editor such as TextEdit.
AppleScript droplet to create symbolic link →
This script asks for a destination folder and creates symbolic links in that folder to whatever you drop onto it. Just save it as an AppleScript droplet, and then drop files or folders onto it that you want to link. The script ignores .webloc files since AppleScript treats those as URLs instead of as files.
AppleScript droplet to map the GPS position of an image in Apple Maps →
This script uses ExifTool by Phil Harvey (a free and open-source command-line application) to extract the GPS position from an image's EXIF data and then displays that position in Apple Maps. Just save it as an AppleScript droplet, and then run the app by dragging an image file onto the app or else by clicking on it and choosing an image file, and if the image has a GPS position in its EXIF data then Maps.app will open with that position as a dropped pin. Read the comments in the script before running it.
AppleScript for Scrivener to sync with external folder and compile →
This script for Scrivener 2 runs the menu commands "Sync with External Folder Now" and "Compile..." so that both can be done with one command. It presumes that "Sync with External Folder" has already been run at least once to set the necessary sync settings. Since the script uses GUI scripting, the Accessibility API must be enabled in OS X 10.8 Mountain Lion and earlier, or it must be specifically enabled for Scrivener in OS X 10.9 Mavericks and later.
AppleScript service to create file with specified creation date →
This script asks for a date and time and then:
- uses the shell command touch to create an empty HTML file in the pre-specified save_path with the creation date set to the user-provided date and time, then
- sets the file extension (.html) of the file to hidden, then
- changes the file metadata so that the file always opens in TextEdit, then
- opens the file in TextEdit, and then
- inserts the creation date into the document body.
Just save it as a systemwide service (a Services menu item) and assign a keyboard shortcut to it, and then press the keyboard shortcut to create and open a TextEdit HTML file with specified creation date. I use this to create journal entries (sometimes in conjunction with WordService's "Insert Short Date & Time" command, free from DEVONtechnologies). The reason why the script prompts for a date is that some of my journal entries were written at an earlier date and time, so I want to be able to specify a creation date other than the current date and time when necessary. It is also possible to use this script with Apple's built-in dictation commands and text dictation for journaling or notetaking entirely by voice. The value of the property save_path and the property resource_fork_path in the script will need to be edited before the script is run; the latter value should be the POSIX path to the file TextEdit.r (included in the same directory as the file you are reading). Before using this script, Apple's Developer Tools must be installed (for example, by running xcode-select --install
or by installing Xcode) so that the Rez and SetFile commands are available. Perhaps the script could be rewritten to use AppleScriptObjC instead of Rez and SetFile, but I don't know how to do that. Read the comments in the script before running it.
AppleScript service to open local URL from hyperlink in TextEdit instead of default reveal in Finder →
This script, as part of an Automator service, opens a local URL (to a file or folder) from a hyperlink in TextEdit. TextEdit's default behavior when clicking on a file:///
hyperlink to a local URL is either (1) to bring the file's window to the front if the file has already been opened in the current TextEdit session or else (2) to reveal the file or folder in the Finder. Using this script as a service provides a third option: right-click (or control-click) on the link and then select the service from the pop-up/contextual services menu to open the file or folder directly. Or assign a keyboard shortcut to the service, then right-click (or control-click) on the link, press the escape key, and press the keyboard shortcut to open the file or folder directly. This service works because when you click on a hyperlink in TextEdit, TextEdit selects the link's text area, which the service passes to a temporary file before extracting and opening the hyperlink.
To create a service from this script:
- open Automator;
- create a new service;
- set the service to receive rich text in TextEdit (or in any application if you want to use the service outside of TextEdit);
- add the New Text File action to the workflow;
- change the settings of the New Text File action to: file format rich text, save as filename OpenFileLinksFromRichText.rtf in location ~/Library/Caches/TemporaryItems/, replacing existing files, with encoding Unicode (UTF-8).
- add the Run AppleScript action to the workflow and select the "Ignore this action's input" checkbox in the Run AppleScript action;
- replace the content of the Run AppleScript action with this script (and replace the path to
python3
in the script with the relevant path to Python 3 on your machine); - save the service.
To do: Currently this script only works when there is one hyperlink in the selected text. I want to modify the script so that it is possible to select multiple links to local files in TextEdit and open them in new TextEdit windows, but that would be such an increase in the complexity of the parsing that it would be better to replace the AppleScript with a Python script and use ready-made Python libraries. I don't write complex AppleScripts for the fun of it; I just want an easy way to accomplish a task!
AppleScript to convert Markdown on clipboard to RTF, then paste →
This script takes plain text from the clipboard in Markdown format, converts the text to Rich Text Format (RTF) using Pandoc and Apple's Cocoa text system, and then pastes the rich text into the active field or window.
AppleScript to convert RTF on clipboard to Markdown, then paste →
This script takes rich text from the clipboard in Rich Text Format (RTF), converts the text to Markdown using Apple's Cocoa text system and Pandoc, and then pastes the Markdown-formatted plain text into the active field or window. The script informs the user if what's on the clipboard is not RTF.