You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript How to make this selection as a taget path?

Hi i bump in to simple things I can't get my head around all the time,

How to make this selection as a taget path?


this code works but only by having a selected folder in finder window selected


so inside my help quotes I can't figure out how to make the path as path I have chosen from the start,


the code simply takes a folders folders and count them and make new folders at a chosen location whit same folder names.


tell application "Finder"

--==========HELP START==========--

--set selected to "/Users/USER/Movies/Test/Effects/" as POSIX file as alias

--set selected to get every item of (entire contents of folder "Macintosh HD:Users:politijagt:Movies:Test:Effects:") whose kind ≠ "Folder"

--reveal selected

set selected to selection

--==========HELP END==========--


set current_folder to item 1 of selected

# Build a list of the files available

set mlist to every folder of current_folder as alias list

# Build a list of the folders available

set theFolders to every folder of current_folder as alias list

set x to mlist

repeat with i from 1 to the count of x

set this_file to item i of x

if i is not 1 then

set previous_file to item (i - 1) of x

set prev_ext to cur_ext

set prev_name to new_name

else

set prev_name to ""

end if

set cur_ext to name extension of this_file

set new_name to (name of this_file as text)

if new_name is not equal to prev_name then

set new_folder to make new folder with properties {name:new_name} at "/Users/USER/Movies/Test/" as POSIX file as alias as string

end if

end repeat

end tell

Posted on Jan 29, 2020 10:30 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 29, 2020 3:50 PM

OK - helps a bit, but let's break it down a bit:


Starting with:


> set selected to “/Users/USER/Movies/Test/Effects/”


In this model, selected is now a string. Nothing more. You can't get a list of files in a string, so we have to coerce it to something else. Also, the Finder predates Mac OS X's UNIX roots, so usually prefers HFS (colon-delimited) paths. If you're providing UNIX paths you need to give the script a hint by adding the 'POSIX file' class (which you do):


> set selected to POSIX file "/Users/USER/Movies/Test/Effects/"


Now UNIX doesn't greatly differentiate between files and folders (hence the 'POSIX file' works regardless of whether you add a file or a folder path, so we need to add one more level of hinting so that the OS knows you're looking at a folder:


> set selected to folder (POSIX file "/Users/USER/Movies/Test/Effects/"


from which you can get every file:


> set selected to every file of entire contents of (folder (POSIX file "/Users/USER/Movies/Test/Effects/")) whose kind is not "Folder"


Now you should have a list of non-folders (eventually - the Finder is pretty bad at running this kind of query).


Does that help?


Now, none of that gets past the issue of the Finder recognizing/honoring symlinks. That's a whole different kettle of fish.


I guess I still don't quite get what you're trying to do, but I realize I might be something to do with your movie production flows.

Similar questions

5 replies
Question marked as Top-ranking reply

Jan 29, 2020 3:50 PM in response to dkaiser92

OK - helps a bit, but let's break it down a bit:


Starting with:


> set selected to “/Users/USER/Movies/Test/Effects/”


In this model, selected is now a string. Nothing more. You can't get a list of files in a string, so we have to coerce it to something else. Also, the Finder predates Mac OS X's UNIX roots, so usually prefers HFS (colon-delimited) paths. If you're providing UNIX paths you need to give the script a hint by adding the 'POSIX file' class (which you do):


> set selected to POSIX file "/Users/USER/Movies/Test/Effects/"


Now UNIX doesn't greatly differentiate between files and folders (hence the 'POSIX file' works regardless of whether you add a file or a folder path, so we need to add one more level of hinting so that the OS knows you're looking at a folder:


> set selected to folder (POSIX file "/Users/USER/Movies/Test/Effects/"


from which you can get every file:


> set selected to every file of entire contents of (folder (POSIX file "/Users/USER/Movies/Test/Effects/")) whose kind is not "Folder"


Now you should have a list of non-folders (eventually - the Finder is pretty bad at running this kind of query).


Does that help?


Now, none of that gets past the issue of the Finder recognizing/honoring symlinks. That's a whole different kettle of fish.


I guess I still don't quite get what you're trying to do, but I realize I might be something to do with your movie production flows.

Jan 29, 2020 2:17 PM in response to dkaiser92

It isn't clear to me quite what you're trying to do here.


If I try to follow the logic backwards from:


> set mlist to every folder of current_folder as alias list


you're trying to get a list of every folder inside of current_folder, where current_folder is defined as:


> set current_folder to item 1 of selected


but, by extension (from your commented code), selected only contains files because you:


> --set selected to get every item of (entire contents of folder "Macintosh HD:Users:politijagt:Movies:Test:Effects:") whose kind ≠ "Folder"


So you set selected to every item of the specified folder that is NOT a folder, then you assume that the first item in the result is a folder. Something here has to give.


Maybe if you could explain your goal it would be easier to understand what's at fault.




Jan 29, 2020 2:59 PM in response to Camelot

My goal is


My end goal is more complex , its like a work around im trying to do because the program that i use wont see some folders as valid symlinks folders. Frome a server to local. I Have checked that every .extention is correct.


local:

topfolder ”symlink” work

topfolders underfolder “symlink” dont work

underfolder files as symlink work


and the reason i just dont use the topfolder as symlink is because i want to merge more folders into some folders inside topfolders under folder.


and as long the program dont see the under folder as a valid symlink folder i need to create a normal folder whit same name that already exists and put the files of that folder inside of it as symlinks.


hope that gave a better idea of my goal.


so my question in this code

have tried a lot of things to get the same action as happens when i do the

set selected to selection

and the code work

but every time i try to choose the path of the folder as

set selected to “/Users/USER/Movies/Test/Effects/”

the code fails

Jan 30, 2020 4:19 AM in response to Camelot

Some times sleep helps my mind , Thx I figured it out whit u help and my fresh mind to get it to work, thx a lot :) its hard to learn on trial and error :)


did like so it works


tell application "Finder"




set selected to folder of entire contents of (folder (POSIX file "/Users/USER/Movies/Test/Effects/"))



set current_folder to item 1 of selected


# Build a list of the files available


set mlist to every folder of current_folder as alias list


# Build a list of the folders available


set theFolders to every folder of current_folder as alias list



repeat with i from 1 to the count of mlist


set this_file to item i of mlist


if i is not 1 then


set previous_file to item (i - 1) of mlist


set prev_ext to cur_ext


set prev_name to new_name



else



set prev_name to ""


end if



set cur_ext to name extension of this_file


set new_name to (name of this_file as text)


if new_name is not equal to prev_name then



set new_folder to make new folder with properties {name:new_name} at "/Users/USER/Movies/Test/" as POSIX file as alias as string


end if


end repeat



end tell

Jan 30, 2020 10:18 AM in response to dkaiser92

> Some times sleep helps my mind , Thx I figured it out whit u help and my fresh mind to get it to work, thx a lot :) its hard to learn on trial and error :)


The Finder has got progressively more complex to script with multiple levels of 'file'-related objects - file... alias... POSIX file... etc. and even now I still stumble through trial and error to get to a functioning script, and I've been doing this a long time lol.

Anyway, glad I could shine some light on it and point you in the right direction.

Applescript How to make this selection as a taget path?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.