So, here I have a snippet of a script that is a folder action. When I drop a folder of images onto the hot-folder, I would like first and foremost to create a new folder (within the dropped folder) called thumbnails… the script will then go on and do other things.
on adding folder items to this_folder after receiving dropped_items
set the_dropped_folder_path to item 1 of dropped_items
set allFiles to list folder (item 1 of dropped_items) without invisibles
set numFiles to count of allFiles
--display dialog "The POSIX path of the folder is: " & (POSIX path of the_dropped_folder_path)
tell application "System Events"
do shell script ("mkdir -p " & (POSIX path of the_dropped_folder_path) & "thumbnails")
end tell
-- ... snip...
end adding folder items to
Seems straight forward enough, I think. However, nothing i do is creating the directory inside of the folder that I drop onto my hot-folder. There is no error generated. If you uncomment the display dialog - the displayed posix path is the proper path - yet it will non create the directory. Any ideas?
on adding folder items to this_folder after receiving dropped_items
set the_dropped_folder_path to item 1 of dropped_items
set allFiles to list folder (item 1 of dropped_items) without invisibles
set numFiles to count of allFiles
--display dialog "The POSIX path of the folder is: " & (POSIX path of the_dropped_folder_path)
do shell script ("mkdir -p " & (quoted form of POSIX path of the_dropped_folder_path) & "thumbnails")
-- ... snip...
end adding folder items to
You need to use the quoted form of the posix path. Also, you don’t need to use the tell application “System Events” block for the do shell script.
set dir to "Macintosh HD:Users:ischeini:Desktop:untitled folder:"
do shell script "/bin/mkdir -p " & POSIX path of dir
Will execute this command:
“/bin/mkdir -p /Users/ischeini/Desktop/untitled folder/”
set dir to "Macintosh HD:Users:ischeini:Desktop:untitled folder:"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of dir
Will execute this command:
“/bin/mkdir -p ‘/Users/ischeini/Desktop/untitled folder/’”
The latter will do what was intended, and creates a folder called “untitled folder” on the desktop. However, the former will create a folder called “untitled” on the desktop, and also a folder called “folder” somewhere else on the hard disk. This depends on the current working directory of the command, and in my case, it appears to be the root level of my hard disk. So it also created a folder /folder. You should probably also check the root level of your hard drive for some extra folders that the script has created.
Actually the command would be better written as:
do shell script "/bin/mkdir -p " & (quoted form of POSIX path of ((the_dropped_folder_path as string) & "thumbnails")))
This also includes the “thumbnails” part in the quoted form the posix path. As “thumbnails” contains no spaces, also the earlier command will work, but if you ever wanted to change it to “thumb nails”, it is smarter to do it the proper way right from the beginning.