Mac keyboard shortcut to copy file path as markdown

The macOS productivity1 shortcut to Copy file/folder markdown path.
How to create macOS Automator Quick Action to copy the file/folder markdown path using shortcut keys as the same way you copy the path name of the file/folder. I give you an idea of how to use JXA in the Automator. As additional information, I introduce how to use macOS Cocoa that will help you to make better logging using macOS Console application.


If you are macOS Automation2 fan to self-improvement of your productivity, the first book to read is Cognitive Productivity with macOS®: 7 Principles for Getting Smarter with Knowledge. This adopt the concepts from the Summary: The 7 Habits Of Highly Effective People by Stephen R.Covey - More knowledge in less time to macOS 👍🏼.

Why is this blog ?

Almost all of my blogs are now written in Markdown format. And most of the productivity tools support the markdown format such as obsidian which is my knowledge management tool where I keep all my blog information before publishing. Therefore, I need to copy locally save files links to the Obsidian the second brain most of the time.
I have created macOS Quick Action service to copy any file or folder location in markdown format.

JXA support

JXA is the new replacement to the AppleScript. JXA is a JavaScript solution for which can be used in macOS script editor as well as Automator tool. For example, to open the last email message:
Mail = Application('Mail')
Mail.activate()

var lastMsg = Mail.inbox.messages[0]
Mail.open(lastMsg)
To use the StandardAddition.osax and its functionality as shown in the dictionary:

Fig.1: Standard Additions
Fig.1: Standard Additions

You have to create the following second line to use the say command.
app = Application.currentApplication()
app.includeStandardAdditions = true
app.say("Hello");
say is a voice command.

Cocoa support

If you are using Cocoa, then as shown in the linen #1 import the library. In the line #6 you can access the Cocoa API.
ObjC.import('Cocoa');
var app = Application.currentApplication();
app.includeStandardAdditions = true;

// access the Cocoa API
var nsDocumentsPath =$("~/Documents").stringByExpandingTildeInPath;

var myDocumentsPath = Path(nsDocumentsPath.js); 
//debugger;
$.NSLog("My document folder is "+ myDocumentsPath.toString());

Logging support

You can find the NSLog output in the Console application.
var app = Application('Finder')
app.includeStandardAdditions = true
var clipStr = app.theClipboard();
debugger;
As shown in the line #4, if you have debugger keyword, that implies you can debug this in the Safari Develop tools. Before use, use you have to enable the develop tool in the Safari and tick the option:"Automatically Show Web Inspector for JSContexts" under your machine name in the Develop menu.

Create Quick Action

As shown in the following Fig.2 you can create Automator Quick Action.

Fig.2: MacOS Automator
Fig.2: MacOS Automator

First and second steps are easy. The 3rd step is given here. Search "JavaScript" component and copy:
function run(input, parameters) {
    
    // Your script goes here
    //debugger;
    const clpPaths = input.map(x => getFilePath(x));
    
    return clpPaths;
}

function getFilePath(clpPath){
    var fileName = clpPath.substring(clpPath.lastIndexOf("/")+1,clpPath.length);
    var mdPath = "["+fileName+"](file:///"+encodeURIComponent(clpPath)+")"
    return mdPath;
}

The function run(input, parameters) should not change because that is what automator expecting to execute.

Testing using macOS Automator

You can save the above as service and assign even shortcut3. As shown in the above line #4, you can debug(uncomment the line) this script while running as a service.

Fig.3: Quick Action Shortcut in context menu
Fig.3: Quick Action Shortcut in context menu

As shown in the Fig.3, In the Mac open the Finder and select one or more files. Now right click and select the Copy File Path or ^ ⬆︎ ⌥ ⌘ c which is the service (it has a shortcut to do the work without a context menu) and v to paste the clipboard contents to your markdown document. Hope this will be really productive. If you need name of the file only in the Finder, then select the fie and press ⌥ ⌘ c.

Reference

Comments

  1. Thank you, works perfectly.

    Looks like a fellow Obsidian user needed the same thing as me :)

    I didn't know about JXA that's awesome!

    ReplyDelete

Post a Comment

commented your blog

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python