Get clipboard path then run script

Normally, I use the OSX finder to navigate to a specific directory and run an AppleScript to create a set of folders. I like to explore the idea of adding the clipboard file path to the script.

The idea is to get the path stored in the clipboard, then let the AppleScript create folders at the path stored in the clipboard. Does this makes sense and is it doable with AppleScript?

Assuming that your existing script works on the front finder window, then it is straightforward.

Example clipboards that should work:
– alias “MDisk:Users:username:Documents:Taskss:”
– folder “Taskss” of folder “Documents” of folder “username” of folder “Users” of startup disk of application “Finder”

tell application "Finder"	
	
	set fw to make new Finder window
	set target of fw to (the clipboard)
	
end tell

If you want, you can reduce it to a single line like this:

tell application "Finder" to set target of (make new Finder window) to the clipboard

Thanks for taking the time to answer my question. I believe the script works when the finder is the frontmost window. I added the snippet to the existing script and get the error:
Finder got an error: AppleEvent handler failed.

Can you please look a the script and let me know if the idea of creating the folders using the clipboard folder path can work with the existing script?

tell application “Finder”

set fw to make new Finder window
set target of fw to (the clipboard)

set theLocation to (target of front window) as string
set folderNames to {"1.Monday", "2.Tuesday", "3.Wednesday", "4.Thursday", "5.Friday", "6.Saturday", "7.Sunday"}
repeat with theFolder in folderNames
	try
		make new folder at theLocation with properties {name:"" & theFolder & ""}
	end try
end repeat

end tell

Your error springs from this line:

Since your script relies on ‘theLocation’ being a location (rather than text) replace ‘string’ with ‘alias’ like so:

set theLocation to (target of front window) as alias

Alternatively, you could use:

set theLocation to insertion location as alias

OK, I try the suggestions and both produced the error
“Finder got an error: AppleEvent handler failed.” number -10000.

When I run the script the line
set target of fw to (the clipboard)
gets highlighted by the OSX.

tell application “Finder”

set fw to make new Finder window
set target of fw to (the clipboard)

set theLocation to (target of front window) as alias
set folderNames to {"1.Monday", "2.Tuesday", "3.Wednesday", "4.Thursday", "5.Friday", "6.Saturday", "7.Sunday"}
repeat with theFolder in folderNames
	try
		make new folder at theLocation with properties {name:"" & theFolder & ""}
	end try
end repeat

end tell

tell application “Finder”

set fw to make new Finder window
set target of fw to (the clipboard)

set theLocation to insertion location as alias
set folderNames to {"1.Monday", "2.Tuesday", "3.Wednesday", "4.Thursday", "5.Friday", "6.Saturday", "7.Sunday"}
repeat with theFolder in folderNames
	try
		make new folder at theLocation with properties {name:"" & theFolder & ""}
	end try
end repeat

end tell

Finder does not need to be visible. As long as the path on your clipboard is a POSIX path, this following code will work just fine. (In Finder, if you control + click on a folder then option + click on “Copy … as Pathname”, the Posix path will be copied)

property pathFromClipboard : missing value
property folderNames : {"1.Monday", "2.Tuesday", "3.Wednesday", "4.Thursday", "5.Friday", "6.Saturday", "7.Sunday"}

copy (the clipboard) to pathFromClipboard

tell application "Finder"
	repeat with i in folderNames
		set name of (make new folder at (pathFromClipboard as POSIX file)) to i
	end repeat
end tell

It’s probably because the clipboard contents do not make for a valid reference. The error-inducing line is expecting something that it can turn into a window target. First, comment out everything below that line, add the following line and run the script again.

[format]set tc to the clipboard[/format]

This will show you what is on your clipboard.

Now, select a folder and make a script with just this and you will see an example of what the clipboard should contain when the full script reaches that line.

[format]tell application “Finder”
set the clipboard to selection as alias
set tc to the clipboard
end tell[/format]

When I run the line set tc to the clipboard
I get the clipboard data: Users/imagegroup/Desktop/SLO

Can you please tell me how to select the folder SLO and create the subfolders?
“1.Monday”, “2.Tuesday”, “3.Wednesday”, “4.Thursday”, “5.Friday”, “6.Saturday”, “7.Sunday”

I am not experienced with AppleScript.

Instead of using my solution from my previous post, this solution will get the path of the current selected folder in the front Finder window and create your new folders there.

property folderNames : {"1.Monday", "2.Tuesday", "3.Wednesday", "4.Thursday", "5.Friday", "6.Saturday", "7.Sunday"}

tell application "Finder"
	set targetFolderPath to selection
	repeat with thisFolderName in folderNames
		set name of (make new folder at targetFolderPath) to thisFolderName
	end repeat
end tell

First off, that is a posix-style reference. A couple things… it should lead with the ‘/’, and if it’s a folder, should have a trailing ‘/’. You can use it but there isn’t really any advantage for what you’re doing.

[format]set ab to POSIX file “/Users/imagegroup/Desktop/SLO/” as alias[/format]

which will return this:

[format]alias “MDisk:Users:imagegroup:Desktop:SLO:”[/format]

But by ‘select’, I meant open a finder window and click once on the SLO folder. Then run the ‘selection as alias’ script, and you will end up with the same:

[format]alias “MDisk:Users:imagegroup:Desktop:SLO:”[/format]

You could also do something like this:

[format]set ab to ((path to desktop) as text) & “SLO:” as alias[/format]

The point is to end up with the correct reference to your desired folder. As you can see, there are multiple approaches you can take. And of course, if you already know the folder, you can just dispense with the clipboard entirely:

[format]set theLocation to alias “MDisk:Users:imagegroup:Desktop:SLO:”[/format]

You just need to clarify how the script will know what your desired folder is.

Hi,

here is one script as starting point to understand workaround:


-- set the clipboard to Finder specifier
tell application "Finder" to set the clipboard to desktop
-- or, set the clipboard to alias 
-- set the clipboard to (path to desktop folder)

try
	set aliasfolder to the clipboard as alias
on error
	tell application "Finder" to set aliasfolder to (the clipboard as specifier) as alias
end try

tell application "Finder"
	try
		make new folder at aliasfolder with properties {name:"subFolder1"}
		make new folder at aliasfolder with properties {name:"subFolder2"}
	end try
end tell

NOTE: if you have already in the clipboard some Finder reference or alias, then setting clipboard code lines can be omitted.

Thanks for your reply. I have been using an AppleScript that creates a set of folders at the selected finder location.

I am trying to edit the script so that the location of the folder set is determined by the path stored in the clipboard. The path in the clipboard originates from the Adobe Bridge app in the following format /Users/imagegroup/Desktop/SLO.

The goal is to get the folder path from Bridge, then have Bridge run the AppleScript to create the folders. Use the path stored by Bridge in the clipboard as the folder destination. Does this make sense?

The script creates subFolder1 and subfolder2 on the desktop.
Is there a way to replace the desktop location with the clipboard path data?

Based on that, you’ll need to ensure that what gets processed has the leading ‘/’ in it. So if it’s included with the Adobe Bridge output, then leave it, but if it’s not, then you need to add it.

So, assuming this is what Adobe Bridge offers, we can set the clipboard to that to test.

set x to "/Users/imagegroup/Desktop/SLO"
set the clipboard to x

set tLocation to POSIX file ((the clipboard)) as alias
--> alias "MDisk:Users:imagegroup:Desktop:SLO:"

set folderNames to {"1.Monday", "2.Tuesday", "3.Wednesday", "4.Thursday", "5.Friday", "6.Saturday", "7.Sunday"}

tell application "Finder"
	repeat with tFolder in folderNames
			make new folder at tLocation with properties {name:tFolder}
	end repeat
	open tLocation -- optional
end tell

NB You don’t need the extra stuff around the name property unless you’re coercing non-text into text.

Also, if the Adobe Bridge stuff is a script (and you intend for both of these to be run in one go), then you don’t need to fool around with the clipboard. Just have Bridge set ‘tLocation’ as alias and let the finder work with that.

First, I believe this is the Bridge script line that copies the path to the clipboard:

app.system('echo '+ thumbs[0].spec.fsName + ‘|pbcopy’);

I try adding the ‘/’ at the end of the line but did not work.

app.system('echo '+ thumbs[0].spec.fsName + ‘|pbcopy’ + ‘/’);

Can the ‘/’ be added to this line?

Second, yes the folder creation starts with a Bridge script that creates a folder set at a specific finder location without having the finder active in the forgeound.
In other words, a Bridge menu item executes the AppleScript to create the folder set. The folder location changes in the finder week by week.

Can you please tell me more about the ‘tLocation’ as an alias method?
I was not aware of this option

Sepa, you are making things difficult for yourself. This is a Direct solution for what you need. Here is the AppleScript code .

property pathFromClipboard : missing value
property folderNames : {"1.Monday", "2.Tuesday", "3.Wednesday", "4.Thursday", "5.Friday", "6.Saturday", "7.Sunday"}

copy (the clipboard) & "/" to pathFromClipboard

tell application "Finder"
	repeat with i in folderNames
		set name of (make new folder at (pathFromClipboard as POSIX file)) to i
	end repeat
end tell

These following images show the results.

Thank you for providing the visual aids. I really appreciate your help with this question.
When I run the script with the clipboard path from Bridge I get an error.
error “Finder got an error: AppleEvent handler failed.” number -10000

Alternatively, I copied the path from Bridge to a text document, added the ‘/’ at the end of the path, then copied the new path back into the clipboard. Then run the script without errors

As you mentioned before the issue is that the copied path from Bridge is missing the ‘/’ at the end.
Can the ‘/’ be added to the Bridge script?
Can AppleScript add the missing ‘/?’
How do you recommend I manage the missing ‘/’?

Thank you!

I updated the code in my previous post. It adds the “/“ to the path. It should work for you now.

property pathFromClipboard : missing value
property folderNames : {"1.Monday", "2.Tuesday", "3.Wednesday", "4.Thursday", "5.Friday", "6.Saturday", "7.Sunday"}

copy (the clipboard) & "/" to pathFromClipboard

tell application "Finder"
   repeat with i in folderNames
       set name of (make new folder at (pathFromClipboard as POSIX file)) to i
   end repeat
end tell

Ok, that makes sense adding ‘/’ via AppleScript. However, I still get the same error when I run the script.
In addition, I was able to add ‘/’ to the Bridge script and still get the same errror.

How are you able to run the script without errors?
Can this be an OSX version difference?

The Bridge script
Without ‘/’
app.system('echo '+ thumbs[0].spec.fsName + ‘|pbcopy’);
Clipboard data:
/Users/imagegroup/Desktop/SLO

With ‘/’
app.system('echo '+ thumbs[0].spec.fsName + ‘/|pbcopy’);
Clipboar data
/Users/imagegroup/Desktop/SLO/

I had a chance to try the script on a second mac computer with OSX Catalina and got the same error.
Next, I try printing the clipboard data with an AppleScript and found out that the ‘/’ is not printing although the script incorporates the ‘/’.

It looks like the ‘/’ is not recognized by the AppleScript.
Is there another way to add the ‘/’ to the clipboard?

The following AppleScript prints this from the clipboard:
"/Users/Shared/WEB
"

property pathFromClipboard : missing value

copy (the clipboard) & “/” to pathFromClipboard

tell application “Finder”

set fw to make new Finder window	
set tc to the clipboard

end tell