Jump to content

Copy file URL (path) and add some characters


Recommended Posts

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

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
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 by deanishe
Link to comment

@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
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 by deanishe
Link to comment
  • 4 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...