Hello,
I’m trying to use the contents of the desktop folder to populate a table. I can get this to work with …
tell application "Finder"
set folderPath to alias "Macintosh HD:User:Jeff:Desktop"
set theFiles to (every file of folderPath)
set theTableViewContents to {}
repeat with a from 1 to length of theFiles
set theCurrentFile to item a of theFiles
set theCurrentFileName to name of theCurrentFile
set end of theTableViewContents to {theCurrentFileName, theCurrentFile as string}
end repeat
end tell
set content of table view 1 of scroll view 1 of drawer 0 of window "Jsetup" to theTableViewContents
HOWEVER, I want to change the folderPath to be relative to the machine it is currently be ran on. How do I go about doing this?
Hi,
the desktop of the current user is the “root” folder of the Finder,
so actually no specifier is necessary, but you can use
tell application "Finder" to set theFiles to every file of desktop
set theTableViewContents to {}
repeat with theCurrentFile in theFiles
tell theCurrentFile to set end of theTableViewContents to {its name, it as text}
end repeat
set content of table view 1 of scroll view 1 of drawer 0 of window "Jsetup" to theTableViewContents
Note: The Finder is only needed to get the files, it’s recommended to avoid large application tell blocks
What if you wanted to specify a folder that is on the desktop?
tell application "Finder" to set theFiles to every file of folder "myFolder" of desktop
For better portability I would add some error handling in case the folder doesn’t exist
try
tell application "Finder" to set theFiles to every file of folder "myFolder" of desktop
set theTableViewContents to {}
-- go on
.
on error
-- do some error handling
end try