Zettelkasten Forum


Open Files in Any App from The Archive (using URL Schemes)

edited May 2020 in The Archive

Hi All,

I’ve been lurking for about a couple of months, but I finally registered for the forum because I’m having a hard time with The Archive software. Whenever I link to an mp3 in my resources subfolder (eg, ![](resources/file name.mp3), I get an error message that states “There is no application set to open the URL resources/file name.mp3” with an option to “Choose Application...” Even if I click that button and then select an application that I know can open the file (eg, VLC), the file still won’t open. Is this a matter of user error (ie, I’m doing something wrong) or is this a limitation of the program?

Any guidance would be very much appreciated.

Best,
Chip

Post edited by ctietze on

Comments

  • edited May 2020

    Welcome!

    If your real files have spaces in the file names, just like your example above, you need to replace the spaces with %20 to make the link a proper URL. Sadly that's how Markdown links are "supposed" to work: by using absolute or relative URLs, be it for web sites or files. (That's why people generally use dashes or underscores in file names on the web instead of spaces.)

    It's hard to bypass this problem to make it easier for users because Markdown usually accepts spaces in the link address part for additional attributes, so ![link text](foo/bar.txt Amazing) becomes <a href="foo/bar.txt" title="Amazing">link text</a> in HTML. Note that the word "Amazing" became the HTML <a>/link tag's title.

    With that in mind, your example ![](resources/file name.mp3) would probably be treated as a link to resources/file with a title of "name.mp3".

    Author at Zettelkasten.de • https://christiantietze.de/

  • Hi Christian!

    Thanks for the welcome as well as the response. Unfortunately, my example autocorrected to file name.mp3 and I didn’t catch it. What I actually meant to write was filename.mp3 without a space. Any idea why I might be having problems given that spacing isn’t actually the issue?

  • Ok, that's weird.

    I also paid closer attention now and just realized that we were talking about ![](), which denotes an image, and not []() for links. Didn't notice that in my last reply :)

    Still:

    • when I add ![](foobar.mp3) and the file does not exist, the app/system beeps when I click on it
    • when I create the file, clicking the link reveals the file in Finder

    Same when I just use a full path file link: <file:///Users/myself/Archive/media/foobar.mp3>

    Can you try to drag & drop the mp3 into The Archive and then prepend file:// (not it'll result in three slashes, ///) and maybe surround the URL with angle brackets?

    Author at Zettelkasten.de • https://christiantietze.de/

  • Dragging and dropping the mp3 into The Archive to get the path and prepending it with “file://” eliminated the error message. Now clicking the link reveals the file in the finder. Thanks for the help!

    Now I’m wondering is there is a way to get the mp3 to play in an application of my choosing when I click the link in The Archive rather than revealing it in the finder?

  • @RedStickChip Good to hear that drag & drop works!

    To open the file in a program, you'd need to replace the file:// portion with a URL scheme handler of your favorite app -- if it supports this way of opening files, that is. Which most don't.

    Adding your own URL scheme handler

    Here's an approach to fix this with scripts.

    • It registers my-vlc:// as a URL scheme
    • The script replaces the my-vlc:// part with file:// and opens your favorite music player (here: VLC)

    I'm using AppleScript Editor. Create new file, paste this script:

    on replace_url_scheme(original)
        return do shell script "echo " & quoted form of original & " | sed 's/my-vlc:/file:/'"
    end replace_url_scheme
    
    on open location this_URL
        set file_URL to replace_url_scheme(this_URL)
        display dialog "I'm doing something with this URL: " & return & this_URL & return & file_URL
        do shell script "open -a VLC " & quoted form of file_URL
    end open location
    

    Save as "Application" on disk. I call mine LaunchVLC.

    Next, we register this "app" with an URL scheme:

    • Locate the application file.
    • Right-click the app, select "Show Package Contents"
    • Inside, open Contents/Info.plist with a text editor
    • Paste the following in a blank line right below the <dict> line:

      CFBundleURLTypes


      CFBundleURLName
      Media Opener
      CFBundleURLSchemes

      my-vlc


    The result should look like this:

    Save the Info.plist.

    Open the LaunchVLC app once to make sure the new URL scheme is registered.

    Now my-vlc:///Users/your_name/file.mp3 should work.

    (Note: You can test this from the Terminal with open my-vlc:///Users/your_name/file.mp3 as well if you're so inclined)

    Download the result

    I whipped this up for others to download. It's code-signed and all, so it should run without problems on Catalina 🤞 Since I signed the scripting app bundle, you cannot change the URL scheme there without breaking the app's integrity (aka it won't launch anymore).

    If you want to customize which app is launched and/or what the URL Scheme is called, I'm afraid you need to go through the steps outlined above yourself.

    Author at Zettelkasten.de • https://christiantietze.de/

  • edited May 2020

    @RedStickChip Good to hear that drag & drop works!

    To open the file in a program, you'd need to replace the file:// portion with a URL scheme handler of your favorite app -- if it supports this way of opening files, that is. Which most don't.

    Adding your own URL scheme handler

    Here's an approach to fix this with scripts.

    • It registers my-vlc:// as a URL scheme
    • The script replaces the my-vlc:// part with file:// and opens your favorite music player (here: VLC)

    I'm using AppleScript Editor. Create new file, paste this script:

    on replace_url_scheme(original)
        return do shell script "echo " & quoted form of original & " | sed 's/my-vlc:/file:/'"
    end replace_url_scheme
    
    on open location this_URL
        set file_URL to replace_url_scheme(this_URL)
        display dialog "I'm doing something with this URL: " & return & this_URL & return & file_URL
        do shell script "open -a VLC " & quoted form of file_URL
    end open location
    

    Save as "Application" on disk. I call mine LaunchVLC.

    Next, we register this "app" with an URL scheme:

    • Locate the application file.
    • Right-click the app, select "Show Package Contents"
    • Inside, open Contents/Info.plist with a text editor
    • Paste the following in a blank line right below the <dict> line:

      <key>CFBundleURLTypes</key>
      <array>
          <dict>
              <key>CFBundleURLName</key>
              <string>Media Opener</string>
              <key>CFBundleURLSchemes</key>
              <array>
                  <string>my-vlc</string>
              </array>
          </dict>
      </array>
      

    The result should look like this:

    Save the Info.plist.

    Open the LaunchVLC app once to make sure the new URL scheme is registered.

    Now my-vlc:///Users/your_name/file.mp3 should work.

    (Note: You can test this from the Terminal with open my-vlc:///Users/your_name/file.mp3 as well if you're so inclined)

    Download the result

    I whipped this up for others to download. It's code-signed and all, so it should run without problems on Catalina 🤞 Since I signed the scripting app bundle, you cannot change the URL scheme there without breaking the app's integrity (aka it won't launch anymore).

    If you want to customize which app is launched and/or what the URL Scheme is called, I'm afraid you need to go through the steps outlined above yourself.

    Download LaunchVLC.zip (Checksum of the zip:15f79e3c4e508c423d699e69d24bcf620c0e3f01707f6c13123af83e49739129)


    Edit 2020-05-19: Fixed code formatting

    Post edited by ctietze on

    Author at Zettelkasten.de • https://christiantietze.de/

  • @ctietze

    I had no idea that it was possible to write custom URL schemes, and I really appreciate your taking the time to teach me how to do it. I've already implemented one for pdfs and have several others that I plan to create. While I already loved using The Archive, the ability to click a link to a file and have it launch in whatever app I want is a gamechanger for me.

    Additionally, I'm shocked that you would take the time to write a script specifically for me so that I can load an mp3 in VLC. That was really above and beyond, and I'm blown away by your generosity.

    Lastly, thank you for putting together this community along with @Sascha. I'm very much looking forward to becoming a contributing member moving forward.

    Sincerely,
    Chip

  • So glad this was helpful! I took the liberty to rephrase the discussion title to better reflect the real culprit: that you wanted to open files, not just reveal them.

    With some more AppleScript-ing, you could write a general purpose "trampoline" that lets you choose the target app -- the path is passed to the Trampoline.app, which asks you for an app to forward the path to, then it executes the open app command.

    Your approach of registering URL schemes for your most used files/apps sounds reasonable! If you figure out anything else that'd be useful, please share so we all can learn :)

    Author at Zettelkasten.de • https://christiantietze.de/

  • An arguably easier to use launcher for macOS that works with openany:// URL scheme to open a file, an an, or a file with an app, from a single link: https://forum.zettelkasten.de/discussion/2798/mac-tool-for-automation-openany/

    Author at Zettelkasten.de • https://christiantietze.de/

Sign In or Register to comment.