However, when I put my folder creation routine inside on adding folder items… it seems to run the script each time for each of the items dropped into the folder at once.
What do I use to just create the folder/date routine once??
Script:
on adding folder items to this_folder after receiving these_items
– Create New Sunday Date Folder
set todaysDate to (current date)
set dd to weekday of (current date)
set dayList to {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
repeat with j from 1 to 7
if dd = (item j of dayList) then
set dayOfWeek to j
exit repeat
end if
end repeat
set dateIncrement to 8 - dayOfWeek
set todaysDate to (todaysDate + (dateIncrement * 60 * 60 * 24))
set {d, m, y} to {day, month, year} of todaysDate
set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if m = (item i of monthList) then
set monthString to text -2 thru -1 of ("0" & i)
exit repeat
end if
end repeat
set yearString to y as text
set yearString to text 3 through 4 of yearString
set dayString to text -2 thru -1 of ("0" & d)
set the_new_folder_name to yearString & monthString & dayString
set newFolderName to (display dialog "Please enter the folder name in yymmdd format" default answer the_new_folder_name)
-- these_items will contain a list of file references to the added items
tell application "Finder"
set myPath to this_folder
if not (exists folder newFolderName) then
display dialog "1"
make new folder in myPath with properties {name:newFolderName}
end if
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
move file this_item to folder newFolderName with replacing
end repeat
end tell
set the item_count to the count of these_items
--display dialog (item_count as string) & "items have been added to folder “" & the_new_folder_name & ¬
-- "”." buttons {"OK"} default button 1
on adding folder items to this_folder after receiving these_items
-- Create New Sunday Date Folder
set todaysDate to (current date)
set dd to weekday of todaysDate
set dayList to {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
repeat with j from 1 to 7
if dd = (item j of dayList) then
set dayOfWeek to j
exit repeat
end if
end repeat
set dateIncrement to 8 - dayOfWeek
set todaysDate to (todaysDate + (dateIncrement * 60 * 60 * 24))
set {d, m, y} to {day, month, year} of todaysDate
set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if m = (item i of monthList) then
set monthString to text -2 thru -1 of ("0" & i)
exit repeat
end if
end repeat
set yearString to y as text
set yearString to text 3 through 4 of yearString
set dayString to text -2 thru -1 of ("0" & d)
set the_new_folder_name to yearString & monthString & dayString
set newFolderName to (display dialog "Please enter the folder name in yymmdd format" default answer the_new_folder_name)
-- these_items will contain a list of file references to the added items
tell application "Finder"
set myPath to this_folder
if not (exists folder newFolderName of myPath) then
display dialog "1"
set newFol to (make new folder in myPath with properties {name:newFolderName})
end if
--repeat with i from 1 to the count of these_items
-- set this_item to item i of these_items
--move file this_item to folder newFolderName with replacing
move these_items to newFol with replacing
--end repeat
end tell
set the item_count to the count of these_items
--display dialog (item_count as string) & "items have been added to folder “" & the_new_folder_name & ¬
-- "”." buttons {"OK"} default button 1
end adding folder items to
Your fix moved the files into the folder. I hadn’t really gotten to debugging that part. I was wondering what is the correct coding architecture for dropping items into a folder action where one has to request information from a user after running some routines.
For example:
If you run mine or your script with say two files it will request the user twice for a folder name. If you drop 3 files it will request user input three times.
It seems as though the line:
on adding folder items to this_folder after receiving these_items
loops through the contained code based upon the number in these_items. Do i have to trap for the first iteration? And if so what is the syntax for that.
I don’t know if wrapping the whole thing in a ‘repeat 1 times’ will help or not.
on adding folder items to this_folder after receiving these_items
repeat 1 times
-- Create New Sunday Date Folder
set todaysDate to (current date)
set dd to weekday of todaysDate
set dayList to {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
repeat with j from 1 to 7
if dd = (item j of dayList) then
set dayOfWeek to j
exit repeat
end if
end repeat
set dateIncrement to 8 - dayOfWeek
set todaysDate to (todaysDate + (dateIncrement * 60 * 60 * 24))
set {d, m, y} to {day, month, year} of todaysDate
set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if m = (item i of monthList) then
set monthString to text -2 thru -1 of ("0" & i)
exit repeat
end if
end repeat
set yearString to y as text
set yearString to text 3 through 4 of yearString
set dayString to text -2 thru -1 of ("0" & d)
set the_new_folder_name to yearString & monthString & dayString
set newFolderName to (display dialog "Please enter the folder name in yymmdd format" default answer the_new_folder_name)
-- these_items will contain a list of file references to the added items
tell application "Finder"
set myPath to this_folder
if not (exists folder newFolderName of myPath) then
display dialog "1"
set newFol to (make new folder in myPath with properties {name:newFolderName})
end if
move these_items to newFol with replacing
end tell
set the item_count to the count of these_items
--display dialog (item_count as string) & "items have been added to folder “" & the_new_folder_name & ¬
-- "”." buttons {"OK"} default button 1
end repeat
end adding folder items to
It seems to want to run the script for each item… vs running the script once with the batch of items. weird… also, it changes the new folder into some japanese symbols.
If this doesn’t work, I’ll break down and set up a test scenario here.
on adding folder items to this_folder after receiving items_
copy items_ to these_items
set items_ to {}
-- Create New Sunday Date Folder
set todaysDate to (current date)
set dd to weekday of todaysDate
set dayList to {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
repeat with j from 1 to 7
if dd = (item j of dayList) then
set dayOfWeek to j
exit repeat
end if
end repeat
set dateIncrement to 8 - dayOfWeek
set todaysDate to (todaysDate + (dateIncrement * 60 * 60 * 24))
set {d, m, y} to {day, month, year} of todaysDate
set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if m = (item i of monthList) then
set monthString to text -2 thru -1 of ("0" & i)
exit repeat
end if
end repeat
set yearString to y as text
set yearString to text 3 through 4 of yearString
set dayString to text -2 thru -1 of ("0" & d)
set the_new_folder_name to yearString & monthString & dayString
set newFolderName to (display dialog "Please enter the folder name in yymmdd format" default answer the_new_folder_name)
-- these_items will contain a list of file references to the added items
tell application "Finder"
set myPath to this_folder
if not (exists folder newFolderName of myPath) then
display dialog "1"
set newFol to (make new folder in myPath with properties {name:newFolderName})
end if
move these_items to newFol with replacing
end tell
set the item_count to the count of these_items
--display dialog (item_count as string) & "items have been added to folder “" & the_new_folder_name & ¬
-- "”." buttons {"OK"} default button 1
end adding folder items to
I’m having an unusual problem with folder actions. I copied the script in this thread and attached it to a folder, when I drop an item into the folder it creates a new folder with the item I dropped in it, but the new folder’s name is in Japanese text??? I’ve seen this happen before with FA’s but never could figure out why it happens. Any guesses!
:shock: Hmm, that’s what jimijon said happened too. I’ve haven’t tested this script but I’m certain that I’ve have never seen or heard of this problem before. How 'bout a OS version report from all affected parties. I guess it’s time for me to test locally.
Ok, I’ve discovered a couple of things. First, the weird naming issue, which happened here too, can be overcome by changing this line:
set newFolderName to (display dialog "Please enter the folder name in yymmdd format" default answer the_new_folder_name)
To this:
set newFolderName to text returned of (display dialog "Please enter the folder name in yymmdd format" default answer the_new_folder_name)
After fixing that, I couldn’t figure out why the script always produced 2 dialogs asking for the name, no matter how many files were added to the folder with the folder action attached. Then it dawned on me that the creation of the new folder is tripping the folder action. If a second batch of files is dropped into the main folder, the second dialog doesn’t appear because the new folder is already present. This bug remains in the script and it could most easily be overcome by creating a permanent folder to hold the dated folders that are created by the folder action. Here’s the semi-improved script.
on adding folder items to this_folder after receiving these_items
-- Create New Sunday Date Folder
set todaysDate to (current date)
set dd to weekday of todaysDate
set dayList to {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
repeat with j from 1 to 7
if dd = (item j of dayList) then
set dayOfWeek to j
exit repeat
end if
end repeat
set dateIncrement to 8 - dayOfWeek
set todaysDate to (todaysDate + (dateIncrement * 60 * 60 * 24))
set {d, m, y} to {day, month, year} of todaysDate
set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if m = (item i of monthList) then
set monthString to text -2 thru -1 of ("0" & i)
exit repeat
end if
end repeat
set yearString to y as text
set yearString to text 3 through 4 of yearString
set dayString to text -2 thru -1 of ("0" & d)
set the_new_folder_name to yearString & monthString & dayString
set newFolderName to text returned of (display dialog "Please enter the folder name in yymmdd format" default answer the_new_folder_name)
-- these_items will contain a list of file references to the added items
tell application "Finder"
set myPath to this_folder
if not (exists folder newFolderName of myPath) then
display dialog "1"
set newFol to (make new folder in myPath with properties {name:newFolderName})
end if
move these_items to folder newFolderName of myPath with replacing
end tell
set the item_count to the count of these_items
--display dialog (item_count as string) & "items have been added to folder “" & the_new_folder_name & ¬
-- "”." buttons {"OK"} default button 1
end adding folder items to
The problem is that each time you create a new folder in the folder with the action attached, you’re adding another item to that folder, which retriggers the folder action. Cure: create the new folders somewhere else. Also, you want the ‘text returned’ of the ‘display dialog’ command.
If you like, here’s a shorter version, employing a couple of tricks from my DateTips package in ScriptBuilders. I’ve assumed here that the new folders are to be created inside another folder - called “Store” - which already exists inside the action folder. You’ll substitute your own destination, of course.
on adding folder items to this_folder after receiving these_items
-- Get next Sunday's date
set todaysDate to (current date)
set SundaysDate to todaysDate - (todaysDate - (date "Sunday, 5 January 1000 00:00:00")) mod weeks + weeks
-- Get that date as a short-date string in yymmdd format
set {d, y} to SundaysDate's {day, year}
copy SundaysDate to b
set b's month to January
set m to (b - 2500000 - SundaysDate) div -2500000
set the_new_folder_name to text 3 thru 8 of ((y * 10000 + m * 100 + d) as string)
-- Confirm that with the user
set newFolderName to text returned of ¬
(display dialog "Please enter the folder name in yymmdd format" default answer the_new_folder_name)
tell application "Finder"
-- If a folder of that name doesn't exist in folder "Store" of this folder, create it
if not (exists folder newFolderName of folder "Store" of this_folder) then
display dialog "1"
make new folder in folder "Store" of this_folder with properties {name:newFolderName}
end if
-- Move all the added items to that folder
move these_items to folder newFolderName of folder "Store" of this_folder with replacing
end tell
set the item_count to the count of these_items
--display dialog (item_count as string) & "items have been added to folder “" & the_new_folder_name & ¬
-- "”." buttons {"OK"} default button 1
end adding folder items to