[SOLVED] Create Finder alias using file in clipboard

I checked this topic, but I can't make it work, because I don't have a set destination such as the Home folder the way the OP had:

So basically what I would like to have is a macro that does this:
Shortcut starts running the macro which copies the path of the selected file/folder to a variable.
Then I go to the destination folder and hit another shortcut and that will create an alias to the file/folder from that variable I just created.

Sometimes having to click and drag a file to a new window to create an alias is not the best workflow so I would like to have this copy-paste type of workflow available as well.

I had a go:

Copy as Alias.kmmacros (23 KB)

Macro screenshot

It's not perfect but it works. See what you think.

1 Like

Thanks for sharing.
AppleScript is still a bit confusing to me so let me ask you:

1 - I want this macro to allow me to pick a different destination, instead of having it set to Desktop. So in this case I would create a variable using the %FrontWindowName% token that I could then use with AS when creating the alias (this would be the destination folder)

2 - I want the macro to pause after copying the path of the file/folder and only when I hit the shortcut (in this case Control+V) will it create the alias
image

I still have a hard time understanding the whole POSIX thing. Sometimes I see it using "as alias" at the end, sometimes without... so I can't really change your script to match what I want.

Also, when I add the section at the top

tell application "Keyboard Maestro Engine"
	set inst to system attribute "KMINSTANCE"
end tell

Do I need to add an extra line per variable I'm using? For example if I have 2 variables I want to use that come from previous actions (let's say Local__Var1 and Local__Var2), will my initial code be like this?

tell application "Keyboard Maestro Engine"
	set inst1 to system attribute "KMINSTANCE"
	set inst2 to system attribute "KMINSTANCE"
end tell

Or that single line is enough and then I just set multiple variables? Like this:

tell application "Keyboard Maestro Engine"
	setvariable "Local__Var1" instance inst to variable1
	setvariable "Local__Var2" instance inst to variable2
end tell

Also, I noticed that you used this "Local__Selection" variable, but there's nothing on the script or the macro itself mentioning it, so I wonder if this is a typo?
setvariable "Local__Selection" instance inst to filePath

I'm also a bit confused about the first and last macros. What are they supposed to do?
What about this? What's the goal here?

-- Simulate Cmd+C to copy the file
		tell application "System Events" to keystroke "c" using command down
		
		-- Delete the alias after copying
		delete newAlias

I was thinking something like this:
Copy as Alias Macro (v11.0.1)

Copy as Alias.kmmacros (30 KB)

Keyboard Maestro Export

The macro I posted creates a temporary alias file. It then copies it (as a file) to the clipboard and deletes it. After the macro has run, you can use ⌘V to paste it wherever you like.

set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set ASVar1 to getvariable "Local__KMVar1" instance inst
	set ASVar2 to getvariable "Local__KMVar2" instance inst
	set ASVar3 to getvariable "Local__KMVar3" instance inst
end tell

I started out by grabbing the selection path as a variable as I anticipated a bit more work beyond the script, but in the end, this wasn't needed. I forgot to clean up unnecessary references to it, but I've updated the macro above now.

Oh I see. I tested and it's working. Thanks!

Ok so "inst" is always the same and then everything else is added based on the each variable. Got it!

Sounds good. Will download it.

Appreciate it. This is something I always wanted to have, because sometimes it's just easier and faster than dragging and dropping files

By the way, what are the first and last actions doing?

@noisneil
I'm experiencing some issues, not sure if you know what's happening?
1 - Sometimes the alias comes with the current time, sometimes without. For example I had the home folder open (I changed it from desktop to home, because all my desktop files are moved automatically) and I saw that the alias didn't have the time when it was created, but when I pasted it I got this:
image

2 - I was testing with some folders and sometimes instead of pasting the alias (which I saw being created and deleted) it started pasting the folder itself as if I hit CMD+C on the folder, not the alias. I'm thinking that maybe the part of the script to delete it is running too fast? I just added a delay of 3 seconds just in case, right before the section to delete the alias, but the issue is still there. It worked the first time I ran it, but then the second time it started pasting the folder itself.

3 - For some weird reason, at one point it got stuck at pasting the same alias, even though I was using the shortcut with other files and folders. So it was pasting the package-lock.json alias, even though I was selecting the Music folder, the Documents folder, etc

Edit:
I did this, but it's showing me an error on the second script

Execute an AppleScript failed with script error: text-script:292:332: execution error: Finder got an error: AppleEvent handler failed. (-10000)

Create Alias from selected file.kmmacros (27 KB)

Keyboard Maestro Export

Any tip on what's wrong?

Yes, I noticed this too. It must be to do with ensuring unique filenames under certain circumstances, and I don't know how to work around it.

Instead of a pause before the deletion, try one before the alias is copied.

When the alias is revealed, a new Finder window will appear if there are none already (for example, if the Finder selection at the start is on the Desktop), which is annoying. This is a basic way to close it if it appears.

Having had time to think, this is probably a better solution:

  1. Select the source file and copy its path. In case you don't already have a macro that does this, here's one:

Copy Path.kmmacros (40 KB)

Macro screenshot

  1. Navigate to your destination directory and run the following AppleScript to create an alias of the original file there:
tell application "Finder"
	try
		-- Get the file path from the clipboard
		set filePath to the clipboard as text
		set originalFile to POSIX file filePath as alias
		
		-- Check if there is an open Finder window
		if (count of windows) > 0 then
			-- Get the current Finder window's target location
			set targetPath to target of front window as string
		else
			-- Use the desktop as the target location
			set desktopPath to path to desktop folder as string
			set targetPath to desktopPath
		end if
		
		-- Create an alias at the target location
		set newAlias to make new alias at targetPath to originalFile
	on error
		-- Display an error dialog if the clipboard does not contain a valid file path
		display dialog "The clipboard does not contain a file path." buttons {"OK"} default button 1
	end try
end tell


Do you need a macro? Finder does this automatically with ⌥ + ⌘ + C
Am I missing something?

Other than that, the script is working. Thanks!

1 Like

Not necessarily, but it's good to cover bases.

1 Like