First problem with your code: thePath is incomplete. AS is returning this as thePath:
startup diskUsers:rototheg:Documents:testScript:
There are two problems with this. First, your path should actually start with the name of your startup disk, not the actual words ‘startup disk’ as a string. Second, in order to construct a valid HFS path, you need to add the “:” after your startup disk name, so What you actually want is code like:
set thePath to ((name of startup disk as text) & ":Users:rototheg:Documents:testScript:")
===NOTE the additional “:” right before ‘Users’. This is because name of startup disk doesn’t return a path itself, just a string.===
…which returns:
nameOfMyDisk:Users:rototheg:Documents:testScript:
Another problem is how you’re using ‘thePath’.
When you say ‘every item in thePath’, what AS thinks you want is the number of items in whatever is contained in thePath the variable. So, its result ends up counting the number of text items in the string thePath, which is 45 in my example case. As in, 45 characters in
myHDname:Users:rototheg:Documents:testScript:
What you want is every item in the folder at the location described by thePath. Like this:
set theItems_count to count of items in folder thePath
Your current error lies in the way you’re using and wording the repeat loop.
repeat with i from 1 to theCount
In this line, you’re setting up the repeat from 1 to however many items are in thePath.
So, the repeat just goes 1, 2, 3… and so on. That, in itself is fine, BUT it doesn’t end up giving you what you want because this line…
set theFileName to item i in folder thePath
… just isn’t valid. What you’re saying with the line above is:
set theFileName to item 2 in folder thePath
- The Finder doesn’t know what “item 2 in folder thePath” means, because it doesn’t keep track of files that way. That line is syntactically invalid.
So, a wordier, but more valid version of your script (what I think you were after in the first place) would be:
tell application "Finder"
set thePath to (startup disk as text) & "Users:rototheg:Documents:testScript:"
set theList to every item in folder thePath
repeat with i from 1 to count of items in theList
set theFile to item i of theList
set theFileName to the name of theFile as text
-- Do more stuff here.
end repeat
end tell