Aliases and Bookmarks on macOS
A question was recently asked about easing the opening of files in a web server.
I contributed the answer below.
…
AppleScript Droplet
Assuming Apache httpd is running and serving documents from a known folder on your local Mac, you could use an AppleScript.
The example AppleScript below expects a file either when run or via drag-and-drop. If the file exists within the server’s root folder, the file’s path is used to open an appropriate URL in the default browser.
on browseFile(theFile)
set serverRootPath to "/Users/yourshortusername" -- absolute path to Apache httpd document root
set serverBaseURL to "http://localhost:8080" -- base URL being served by Apache httpd
set filePath to (the POSIX path of theFile)
if filePath begins with serverRootPath then
set fileURL to findAndReplaceInText(filePath, serverRootPath, serverBaseURL)
--display dialog (quoted form of fileURL) -- uncommment to aid debugging
do shell script "/usr/bin/open " & quoted form of fileURL
else
-- file is outside if the web server's root directory
display dialog "File is not known to server: " & filePath
end if
end browseFile
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
on open of finderObjects -- "open" handler triggered by drag'n'drop launches
repeat with i in (finderObjects) -- in case multiple objects dropped on applet
browseFile(i)
end repeat
end open
browseFile(choose file with prompt "Select a file:") --if double-clicked
See Apple’s Processing Dropped Files and Folders and Ben’s AppleScript Snippets for the file handling code used above.
To use this script, copy and paste the script above into macOS’s Script Editor. Run, test, and modify the script within Script Editor.
- Applications > Utilities > Script Editor.app
When the script is working as desired, File > Export… the script as an application. The resulting application will accept dropped files. Place the application in your Dock or elsewhere.
Beyond a Droplet
This approach can be extended with Automator to create a Finder service. This would allow right-click launching the of the script and a keyboard shortcut in the Finder.
A Finder service is beyond the scope of this question. Feel free to ask questions about creating macOS services once this script is proven as helpful.
I originally published this answer on Ask Different.