Make a Finder Alias Using a Keyboard Maestro Variable

Hi,

I'm trying to get the current path and create an alias to my home folder.

I can get this to work when it's hardcoded but I'm struggling to pass current folder as a Keyboard Maestro variable into the shell script.

Can anyone help?

Thanks.

1 Like

Hey Jay,

Any special reason you're not doing this job all in AppleScript?

set homeFolderAlias to path to home folder

tell application "Finder"
   set newAliasTarget to insertion location as alias
   make new alias file at homeFolderAlias to newAliasTarget
end tell

-Chris

Thanks Chris! No reason, other than I didn't know how to do it in Applescript. I've tried that and it works great! For flexibility, how can I change the path to a different folder? e.g. Downloads folder rather than home folder

Hey Jey,

path to downloads folder

To find out all the available “path to” folders run this in the Script Editor.app.

set standardAdditionsOsaxPath to (path to library folder from system domain as text) & "ScriptingAdditions:StandardAdditions.osax"

tell application "Script Editor"
   activate
   open standardAdditionsOsaxPath
end tell

Then search for "path to".

You'll find two "path to" items, and the second one will have all the available locations.

You can also hard-code locations.

It's best to use aliases (different than Finder aliases) when talking to the Finder with AppleScript.

Here's an example:

set myPOSIXPath to "~/Downloads"

# Expand the tilde in $HOME-based path strings.
tell application "System Events"
   set myPOSIXPath to POSIX path of disk item myPOSIXPath
end tell

set targetFolderAlias to alias POSIX file myPOSIXPath

Clear as mud?   :sunglasses:

-Chris

1 Like

Thanks for your help Chris.

If I just run the following in AppleScript, say while my 'Dropbox' folder is selected in Finder, nothing appears in my Downloads folder. Am I missing something? :confused:

set myPOSIXPath to "~/Downloads"

# Expand the tilde in $HOME-based path strings.
tell application "System Events"
   set myPOSIXPath to POSIX path of disk item myPOSIXPath
end tell

set targetFolderAlias to alias POSIX file myPOSIXPath

Hey Jay,

Yep.

All I was showing you there was how to get the alias to a hard-coded destination. It wasn't a complete script.

Try this:

set targetFolderPosixPath to "~/Downloads"

# Expand the tilde in $HOME-based path strings.
tell application "System Events"
   set targetFolderPosixPath to POSIX path of disk item targetFolderPosixPath
end tell

set targetFolderAlias to alias POSIX file targetFolderPosixPath

tell application "Finder"
   set newAliasTarget to insertion location as alias
   make new alias file at targetFolderAlias to newAliasTarget
end tell

-Chris

Ah ok, sorry for being a little dense! :sweat_smile:

Thanks so much for your help—this works great.

Whups.

I should correct myself.

That code doesn't show a hard coded destination – it's a volatile $HOME-based path.

So it would work on anyone's system and produce the same relative destination.

A truly hard-coded path would look like this:

"/Users/User-Name/Downloads"

-Chris

Hi Chris,

Thank you for assisting Jay in getting this script to work, it is working for me beautifully in MacOS Mojave but it doesn't appear to be working in MacOS Catalina (beta). I wonder if this could have something to do with Privacy permissions or the way Apple Script works in the new OS.

I understand you might not have the new OS installed as yet and I know that is in beta at the moment but if you could make any suggestions as to what I could check to see if I can get it working in Catalina would be really appreciated.

Hey David,

Have you tried running it from Apple's Script Editor.app?

What if you run the code without the Finder-Tell-Block?

-Chris

Chris,

Before I had a chance to test it, Apple updated Catalina Beta and it fixed the problem!

So I'm delighted, thank you for your expert advice and special thanks to Jay for creating the macro.

Hey David,

I noticed just today on another forum that this was a known bug in Catalina.

-Chris