I have been messing with some code. It is not even good enough to post here because I am sure I am going about it the wrong way. I am looking to select a folder and have a script look only at the first level inside that folder and grab the name of every item(files and folders) and set it to a list.
I don’t even want to include my code because I think it is bloated and I am going about it the wrong way.
What I am looking for a simple handlers such as
on contentNames(folderToCheck)
tell application finder
set myList to name of every item in folder folderToCheck
end tellx
end contentNames
The issue I think I am having now(though my code works, just very slow), I think is due to using a loop where I should not be. My code takes a selected folder and counts the number of items. I then pass that value to a handler that gets the name of item x via a loop. It just takes way longer then I think it should have to.
Is there a way to just tell apple script to just give me every item name as a list?
Thanks a ton, and may the Lord Jesus Christ Bless you sincerely unto salvation!
I kept doing some searching and I found this post(link below). I plan to test it a bit and see if it works for me, but I think this is what I am looking for.
http://macscripter.net/viewtopic.php?id=17449
Your code was almost exactly what you needed. I just basically added the selection part and some error checking.
tell application "Finder" to set theSelection to (selection) as alias list
if theSelection is not {} then
set firstSelectedItem to item 1 of theSelection
set namesList to contentNames(firstSelectedItem)
else
error "Error: please select something in the front Finder window before proceeding."
end if
on contentNames(folderToCheck)
set folderToCheck to folderToCheck as text
tell application "Finder"
return name of every item in folder folderToCheck
end tell
end contentNames
Thanks, I was not aware of being able to set a selection like that, I only knew of the dialog box and droplet application.
Your code also seams more concise.
Thanks a lot! Greatly appreciated!
It will not work if the item selected is an alias. The end users of this script will be running from an alias of a dir on a server. Any ideas for a fix?
This is the other version I was trying, I don’t know which code is better as both seem to work, with the exception of your code not accepting an alias.
Also do you see either version being better or potentially faster as the folders we will be getting the contents of may have even thousands of folders inside(Most often more like 300 to 800).
global FNames
set FNames to ""
on open docList
set docFolder to POSIX path of docList
if docFolder ends with "/" then
setClip(docList)
else
display dialog "This program only works with folders."
end if
end open
set docList to (choose folder)
setClip(docList)
on setClip(docList)
set dl to docList as alias
tell application "Finder"
--set theList to name of every file of folder dl
set theList to name of every folder of folder dl
repeat with aFile in theList
set FNames to FNames & aFile & return
end repeat
set the clipboard to FNames
display dialog FNames
end tell
end setClip
FNames
Thanks again, and God bless you!
I have the script working, I changed some handler and variable names around a bit so I could understand it better. Is there a way to make this work with multiple folders? This is what I have so far…
global fileNames
set fileNames to ""
--Use: Droplet and check if item is a folder, else error.
on open checkItemIsFolder
set folderToCheckAsPOSIX to POSIX path of checkItemIsFolder
if folderToCheckAsPOSIX ends with "/" then
setItemList(checkItemIsFolder)
else
display dialog "This program only works with folders."
end if
end open
--Use: Creates a list of all the folder in a folder.
on setItemList(checkItemIsFolder)
set dl to checkItemIsFolder as alias
set fileNames to ""
tell application "Finder"
--set theList to name of every file of folder dl
set itemList to name of every folder of folder dl
repeat with aFile in itemList
set fileNames to fileNames & aFile & return
end repeat
set the clipboard to fileNames
display dialog fileNames
end tell
end setItemList
--Use: Will only let you select a folder.
set checkItemIsFolder to (choose folder)
setItemList(checkItemIsFolder)
fileNames
Hi,
try this, the result is copied to the clipboard with the folder path at the head of each list
on run
open (choose folder with multiple selections allowed)
end run
--Use: Droplet and check if item is a folder, else error.
on open droppedItems
set fileNameList to {}
repeat with anItem in droppedItems
set {folder:Fo, package folder:Pa} to info for anItem without size
if Fo and not Pa then -- process item only if it's a folder
set end of fileNameList to setItemList(anItem)
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, (return & return & return)}
set the clipboard to (fileNameList as text)
set text item delimiters to TID
end open
--Use: Creates a list of all the folder in a folder.
on setItemList(folderAlias)
set fileNames to ""
tell application "Finder"
set itemList to name of every folder of folderAlias
end tell
set beginning of itemList to ((folderAlias as text) & return)
set {TID, text item delimiters} to {text item delimiters, return}
set itemList to itemList as text
set text item delimiters to TID
return itemList
end setItemList
Here’s a script that works with multiple items.
Some comments:
- in your code line “on open checkItemIsFolder” checkItemIsFolder is a list of items. Since it’s a list you have to get the items of the list individually. You are not doing that and are lucky your script worked. If you dropped more than one item on the droplet it would have errored.
- Your code for checking if it’s a folder will not work. For example a package file’s posix path will end in a “/” but it is not technically a folder that you can get the sub-folders from. So again in this case you script would error.
-- this happens when you run (double-click) the droplet
on run
--Use: Will only let you select a folder. You can select multiple folders.
set chosenFolders to choose folder with multiple selections allowed
checkEachFolder(chosenFolders)
end run
-- this happens when you drop files on the droplet
-- NOTE: droppedItems is a list of dropped items
on open droppedItems
-- ensure each dropped item is a folder
set folderList to {}
repeat with anItem in droppedItems
tell application "System Events" to set theKind to kind of anItem
if theKind is "Folder" or theKind is "Volume" then set end of folderList to anItem
end repeat
if folderList is not {} then
checkEachFolder(folderList)
else
display dialog "This program only works with folders."
end if
end open
on checkEachFolder(folderList)
repeat with aFolder in folderList
setItemList(aFolder)
end repeat
end checkEachFolder
--Use: Creates a list of all the folders in a folder.
on setItemList(checkItemIsFolder)
set dl to checkItemIsFolder as alias
set fileNames to ""
tell application "Finder"
set itemList to name of every folder of folder dl
if itemList is {} then
set fileNames to "There are no folders in this folder!"
else
repeat with aFile in itemList
set fileNames to fileNames & aFile & return
end repeat
end if
end tell
set the clipboard to fileNames
display dialog fileNames
end setItemList
Thanks Guys!
This will be really handy.
Have a great weekend.
cindy in indy