I have one directory with many sub directories each containg photos from various occasions.
Rather than create all the albums for those folders manually, i want to write a script to do it automatically in iphoto.
i keep getting the error:
cant make <> “photoname.jpg” of <> “directoryname” of <> “main directory” of application “finder” inot the expected type
how do i resolve this issue? my script is shown below:
– get source folder
set sourceFolder to choose folder with prompt “selecct a folder…”
– create a list
set FolderList to {}
set pics to {}
tell application “Finder”
set FolderList to every folder in sourceFolder
set counterVar to 1
set NumOfAlbums to length of FolderList
repeat while NumOfAlbums ≥ counterVar
set newFolder to item counterVar of FolderList
set stringName to (get name of newFolder)
tell application "iPhoto"
if not (exists album stringName) then
new album name stringName
end if
set stringName to (get name of newFolder)
--get all photos in the folder and add to new album
set pics to every file in newFolder where name extension is "jpg"
add pics to album stringName
end tell
counterVar = counterVar + 1
end repeat
the command to import photos to an album is import from not add
and the class of the operand must be either alias or POSIX path
Try this
set sourceFolder to choose folder with prompt "select a folder..."
tell application "Finder" to set FolderList to every folder in sourceFolder
repeat with oneFolder in FolderList
set stringName to name of (info for oneFolder as alias)
tell application "Finder"
try
set pics to (files of oneFolder whose name extension is "jpg") as alias list
on error
set pics to (files of oneFolder whose name extension is "jpg") as alias
end try
end tell
tell application "iPhoto"
if pics is not {} then
if not (exists album stringName) then new album name stringName
import from pics to album stringName
end if
end tell
end repeat
pics is a list of elements e.g. {pic1, pic2}.
{} signifies an empty list
exactly
alias is a class in AppleScript to specify files and folders. It’s similar to an alias in the Finder but not the same.
It always points to the original file or folder
Just to clarify this further: the ‘files’ and ‘folders’ returned by the Finder are in forms called Finder references, which only the Finder can understand. In Stefan’s script, they’re coerced to the more generic AppleScript form ‘alias’ so that they can be understood by ‘info for’ (a StandardAdditions command) and by iPhoto. In fact, these two lines.
set stringName to name of (info for oneFolder as alias)
tell application "Finder"
. could be rendered instead as:
tell application "Finder"
set stringName to name of oneFolder
An ‘alias’ in AppleScript shouldn’t be confused with an ‘alias file’ in the Finder. The latter’s the Finder’s scripting term for an ‘alias’ file that acts as a proxy for another file or folder located somewhere else on disk.
thanks for the clarification guys. it really has helped me understand this quite a bit more. i still have an issue. i didnt just copy and paste the code provided to me here, as i wanted to learn, so i wanted to mod my code to work. now, my code creates about half the albums, and populates most of them with the folders and then when i get to photo number 404 i get this error:
cant make alias “path…pickname.jpg” into type <>.
the photo is no different than any of the other ones so i dont know why this one is causing this error. i removed this photo, but a different photo a bit further on causes the error. any ideas?
my new script is this:
– get source folder
set sourceFolder to choose folder with prompt “selecct a folder…”
set pics to {}
tell application “Finder”
set FolderList to (every folder in sourceFolder)
repeat with newFolder in FolderList
set stringName to (get name of newFolder)
tell application "Finder"
set pics to (every file in newFolder where name extension is "jpg") as alias list
end tell
tell application "iPhoto"
set stringName to (get name of newFolder)
if not (exists album stringName) then
new album name stringName
end if
import from pics to album stringName
end tell
end repeat
...
tell application "Finder"
try
set pics to (files of oneFolder whose name extension is "jpg") as alias list
on error
set pics to (files of oneFolder whose name extension is "jpg") as alias
end try
end tell
...
the coercion as alias list works only with multiple items,
one item causes an error. The try block catches the error and coerces a single
item to an single alias
cant believe i missed that. my only excuse is its now 12 am here and im not home that long after a long day at work. thanks a mil for not taking the mick out of me for not noticing that. and thanks for the help.
Minor thing, You still want the one item to be a one item list. Or you dealing with multiple class later, a pain. Just add as list.
tell application "Finder"
try
set pics to (files of oneFolder whose name extension is "jpg") as alias list
on error
set pics to (files of oneFolder whose name extension is "jpg") as alias as list
end try
end tell