check if folder exists, but the name is variable.

Hello all,

Oh, how I wish I could write code. I work at a school, and I am making a droplet for the secretary. She’s going to take one or more images, drop them on the droplet, and they will get scaled to web size and copied via AFP to a folder on the web server.

The problem I am having is that I am trying have a folder created for each week of the year. I want to check for the existence of this folder before it tries to create it. Here’s the code that isn’t working:


if not (exists folder ("sharepoint:path:images:stories:week ") & (weekNum) & (" pics")) then

it throws the following error: Finder got an error: Can’t get folder "sharepoint:path:images:stories:week ".

I can’t figure out how to get the variable weekNum & the word pics into the patch check. Any help is SO appreciated.

The entire script is below. Thanks!

-Dave


--if they double-click it, gently chastise them
on run
	display dialog "No no no!  Don't double-click me...drag pictures on top of me!" buttons {"DOH!"} default button 1
end run
-----------------------------------------------------------------
on open draggedItems
	--mount share point
try
		mount volume "afp://server/share" as user name "user" with password "pass"
	end try
	-----------------------------------------------------------------	
	--define the pics folder
	delay 5
	set pics_folder to "share:path:to:image:folder"
	-----------------------------------------------------------------		
	--get name of weekday
	set current_week_day to weekday of (current date) as text
	
	--use unix to get week number
	set weekNum to do shell script "date +%U"
	-----------------------------------------------------------------	
	
	--if it is not Monday (the day the newsletter
	--goes up), add a number to the weekNum
	if current_week_day = "Monday" then
		set weekNum to weekNum
	else
		set weekNum to "0" & weekNum + 1
	end if
	-----------------------------------------------------------------	
	
	--check to see if this "week number" folder already exists
	tell application "Finder"
		if not (exists folder "share:path:to:image:folder " & weekNum & " pics") then
			
               -- it doesn't exist...make a folder with the name of the week number
			set foldername to "week " & weekNum & " pics"
			tell application "Finder" to make new folder at pics_folder
			set newfolder to the result as alias
			tell application "Finder" to set the name of newfolder to foldername
			
		end if
	end tell

end open

Hi Dave,

try this, I changed the code to compose the week number reliably, to abort the script
if the volume couldn’t be mounted and to create the folder with a shell command.
The delay is not needed. The script waits until the volume is available.
I added properties for the immutable data to test it


property theVolume : "share"
property share : "afp://user:pass@server.local/" & theVolume
property pics_folder : theVolume & ":path:to:picfolder:" -- the leading / trailing colons are necessary

--if they double-click it, gently chastise them
on run
	display dialog "No no no!  Don't double-click me...drag pictures on top of me!" buttons {"DOH!"} default button 1
end run
-----------------------------------------------------------------
on open draggedItems
	if theVolume is not in (do shell script "/bin/ls /Volumes") then
		try
			mount volume share
		on error
			return
		end try
	end if
	--use unix to get week number
	set weekNum to do shell script "date +%U"
	-----------------------------------------------------------------	
	
	--if it is not Monday (the day the newsletter
	--goes up), add a number to the weekNum
	if weekday of (current date) is not Monday then set weekNum to weekNum + 1
	set weekNum to text -3 thru -1 of ("00" & weekNum)
	
	-----------------------------------------------------------------	
	--check to see if this "week number" folder already exists
	-- use the shell mkdir command to create the folder
	-- The -p flag ignores a message if the folder exists

	do shell script "/bin/mkdir -p " & quoted form of (POSIX path of pics_folder & "week" & weekNum & "pics")
	
end open

Steffan, once again you have given me an answer with some very slick code to study. It works perfectly, and SO FAST.

In your debt once again,

Dave

p.s. do you have any commercial endeavors out there we can check out? I’d love to be able to float you some business if possible.

Effectually, you seem to want Tuesday-start weeks. I don’t know if they’re possible with a shell script, but they certainly are with AppleScript.

One problem with the above code (Stefan’s fixed the other) is that if you run it in the last week of the year, the week number produced will be one more than the number of weeks in the year. I imagine you really want it to be “01”.

set today to (current date)

-- Get 4th of January this year, and from that, the start of the first Tuesday-start week of this year.
set Jan4 to date "Saturday 4 January 1000 00:00:00"
set Jan4's year to today's year
set week1Start to Jan4 - (Jan4 - (date "Tuesday 7 January 1000 00:00:00")) mod weeks

-- Get 4th of January next year, and from that, the start of the first Tuesday-start week of next year.
-- The difference between the two first Tuesdays is the number of Tuesday-start weeks this year.
set Jan4's year to (Jan4's year) + 1
set maxWeeks to (Jan4 - (Jan4 - (date "Tuesday 7 January 1000 00:00:00")) mod weeks - week1Start) div weeks

-- Calculate the current week number and mod by the number of weeks in this year.
-- Add one to get next week's number and another 100 to get a three-digit result.
-- Coerce to text and drop the first digit from it.
set weekNum to text 2 thru 3 of ((((today - week1Start) div weeks + 1) mod maxWeeks + 101) as Unicode text)

This is about twenty-five times as fast as the original code, although the difference is unlikely to be noticeable in practice. :wink:

Nice! Thanks to both of you!

-Dave

Oops! Today I think my weekNum results are 1 too high. I’ve corrected my code above.

It turns out that while comparing my script’s result’s with the original’s, I was being confused by Unix’s idea of a “week number”. I’d assumed it would conform to the ISO standard whereby weeks begin on Mondays and a week straddling a year boundary belongs to the year in which it has the most days. From that point of view, my script was right first time and I’ve now restored it to the way it was. Its Tuesday-start approach is arranged so that it returns 1 more than the current ISO week number on every day except Monday.

“date +%U”, however, starts the year returning “00” and increases this to “01” on the first Sunday of the year. The number increases every Sunday thereafter but goes back to “00” on whatever day of the week the next New Year’s Day occurs ” unless that’s a Sunday, in which case the number goes back to “01”. Adding 1 to these results every day except Monday, as in Dave’s original script, doesn’t make sense, as the end result goes up on Sunday, comes down again on Monday, then goes back up on Tuesday.