Mikeatalfred Posted April 28, 2021 Share Posted April 28, 2021 I am using Obsidian as a PKB and find myself doing quite a lot unnecessary pdf file copying from my reglar storage of pdf files to obsidian's attachment folder. To stop this I decided to copy the file's actual URL and paste the file path into my obsidian markdown file, and avoid the double storage. Copying the URL is easy, option-right-click and select Copy as Filepath, and then past the URL into obsidian. But I need to add extra characters to the URL every time to make it useful/clickable in Obsidian. So I like to automate this a bit. I am sure there are a lot of similar use cases apart from obsidian but I cannot find simple answer. This is what I would like to do 1. Select a file name in Finder - or right click the file name and select a Service 2. Retrieve the file URL / file path 3. prepend the URL with "[](<file:///" 4. add the URL after these characters 5. append ">)" 6. add this string (combining step 3, 4 and 5) to the clipboard This way I will be able to select or click a file name in Finder, for example /Users/mike/Downloads/random_filename.pdf And this action would add this to my clipboard, a very useful link to my file in Obsidian [](<file:////Users/mike/Downloads/random_filename.pdf>) Anyone who can point me toward a solution for this, or maybe there is much smarter way of doing what I am trying to achieve. Thanks a lot /Mike Link to comment
giovanni Posted April 28, 2021 Share Posted April 28, 2021 If you link a hotkey to a 'run script object' set to applescript and with the code below, then copy that to clipboard, this should do what you are after. You would press the hotkey and this would apply to the file currently selected in Finder (or you can restrict the workflow scope to Finder). You can also use the text in the clipboard before the action and add it to the square brackets (e.g. if `myfile` is in the clipboard you would get `[myfile](myfilelink)`) hope this helps on run argv tell application "Finder" get selection as alias set aliasItemName to selection as alias set posixPathName to POSIX path of aliasItemName end tell return ("[](<file:///" & posixPathName & ">)") end run Link to comment
deanishe Posted April 28, 2021 Share Posted April 28, 2021 (edited) 11 hours ago, Mikeatalfred said: Copying the URL is easy I'm afraid it's not that simple. You're copying a filepath, not a URL, and they have different rules, so you can’t just stick file:// in front of a path to make a URL. Sometimes it works, but a whole bunch of characters that are allowed in paths are not allowed in URLs. You need to URL-encode the path to get a valid URL, and in your case, it needs additional processing to ensure it works with Markdown (closing parentheses are allowed in URLs, but need escaping or encoding inside Markdown links or the URL will be truncated at the first closing parenthesis). Here's a JXA function you could stick in a File Action (which is the best way to pass a selected file to Alfred): // convert POSIX path (e.g. /path/to/file) to a Markdown URL function markdownUrl(path) { // encode path for use in a URL let url = 'file://' + encodeURI(path) // also encode parentheses to ensure a valid Markdown URL url = url.replace('(', '%28').replace(')', '%29') // return Markdown-formatted URL return `[](${url})` } function run(argv) { // first command-line argument is the filepath return markdownUrl(argv[0]) } Put it in a Run Script Action with Language = "/usr/bin/osascript (JS)" and with "input as argv" Edited April 29, 2021 by deanishe Floating.Point and giovanni 2 Link to comment
giovanni Posted April 28, 2021 Share Posted April 28, 2021 or you might want to try Hook, a nice app with an Alfred-like interface to create bidirectional links between files/webpages. Grug 1 Link to comment
Mikeatalfred Posted April 29, 2021 Author Share Posted April 29, 2021 @giovanni - thank you for your suggestion. I've tried it and got an error that went a bit over my head: [13:27:59.980] Copy filepath to obisidan[Hotkey] Processing complete [13:27:59.982] Copy filepath to obisidan[Hotkey] Passing output '( "/Users/micke/Downloads/skiss_stenbolaget.jpg" )' to Run NSAppleScript [13:27:59.984] ERROR: Copy filepath to obisidan[Run NSAppleScript] { NSAppleScriptErrorNumber = "-1708"; } [13:27:59.984] Copy filepath to obisidan[Run NSAppleScript] Processing complete [13:27:59.985] Copy filepath to obisidan[Run NSAppleScript] Passing output '' to Copy to Clipboard After som googling on the -1708 error and some testing I just excluded the script and pasted the output from the Hotkey into Copy to Clipboard {query} and added the characters I needed. This works as a charm :-). So thanks a lot, my workflow just got improved! /Mike Link to comment
Mikeatalfred Posted April 29, 2021 Author Share Posted April 29, 2021 @deanishe - thank you, the error I described above (-1708), I guess, is just what you predicted. In this specific use case (Alfred and Obsidian) a very simplistic workflow does the job. I guess the <> preserves the path so Obsidian understand it. I will give your suggested an approach later Thanks a lot! Link to comment
Mikeatalfred Posted April 29, 2021 Author Share Posted April 29, 2021 @giovanni - Hook look interesting, never heard of it before. Thanks Link to comment
deanishe Posted April 29, 2021 Share Posted April 29, 2021 (edited) 1 hour ago, Mikeatalfred said: the error I described above (-1708), I guess, is just what you predicted That's an AppleScript error. The problem I'm talking about is the syntactic validity of your URLs/Markdown links. You might be able to fix the AppleScript error by not using Run NSAppleScript. If you need to run AppleScript, use a normal Run Script with Language = /usr/bin/osascript (AS). 1 hour ago, Mikeatalfred said: I guess the <> preserves the path so Obsidian understand it. No. Like I said, sometimes your URLs will be valid because the filepath you copied doesn't contain any characters that are invalid in a URL. This is the case with the path you posted above. Edited April 29, 2021 by deanishe Link to comment
giovanni Posted May 1, 2021 Share Posted May 1, 2021 FYI @Mikeatalfred these Markdown Notes by @Acidham include an `Upload Asset for MD Notes` file action which could be helpful to you Link to comment
giovanni Posted September 13, 2021 Share Posted September 13, 2021 On 4/28/2021 at 4:37 PM, deanishe said: Here's a JXA function you could stick in a File Action (which is the best way to pass a selected file to Alfred): @deanishe this is super helpful, thank you! The script currently adds a new line at the end of the string (or at least there is one when copied to clipboard), any chance it is possible to avoid that? Link to comment
deanishe Posted September 14, 2021 Share Posted September 14, 2021 (edited) 4 hours ago, giovanni said: any chance it is possible to avoid that? It’s not the script, it’s /usr/bin/osascript adding the newline, so no, it can’t be avoided I'm afraid. You'll have to trim the string afterwards if the newline is a problem. Edited September 14, 2021 by deanishe giovanni 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now