Sumeet Mourya
Sumeet Mourya

Reputation: 434

can't get the items of a folder in list through Applescript

I am not getting the file inside the images folder through this code. How can I get that file's name in the given variable (filelist)?

set p to "/Users/sumeet/Desktop/images/"

set a to POSIX file p

set filelist to (names of items of (POSIX file a))

-- file "Macintosh HD:usr:local:bin:"

Upvotes: 2

Views: 6371

Answers (2)

Rob Napier
Rob Napier

Reputation: 299545

set p to "/Users/sumeet/Desktop/images/"
set a to POSIX file p
tell application "Finder" to set filelist to name of items of folder a

You had a few mistakes. First, you tried to take the POSIX file of a file object (a). You only use this once. Second, you used names of items when you actually meant name of items. Your way makes more sense grammatically, but is incorrect Applescript. Third, you have to ask Finder to do this for you, since you want to look at the file system.

Upvotes: 1

Kassym Dorsel
Kassym Dorsel

Reputation: 4843

This will work :

set a to "Macintosh HD:Users:sumeet:Desktop:images:" as alias
tell application "Finder"
    set filelist to name of every file in a
end tell

You have to tell Finder to get the list of items. Also Finder only takes alias paths, so you have to change the unix path to an alias (aaa:aaa:aaa:) format.

You could also do it this way :

tell application "Finder"
        set filelist to name of every file in folder "images" of desktop
    end tell

Upvotes: 2

Related Questions