Multiplying seconds with variable

I had this working earlier but somehow I lost it.

In this script I change the creation dates to a series of new dates with user-definable intervals. Two things I haven’t been able to figure out yet.

  1. How to multiply a variable with ‘minutes’, ‘seconds’ etc.
  2. If the files are loaded, the order with which they chance should be based on their current file name. I have no idea how to accomplish that.

Any help would be appreciated by this newbie

PS StefanK, thanks for the help earlier!

The code:

property incrAmount : missing value
property incrUnit : missing value
property lcUnit : missing value
property incrMultiplier : missing value
property newMultiplier : missing value
property newCreationDate : missing value

on open fileList
	set dialogMsg to "Please enter new creation (DD MM YYYY):"
	set newCreationDate to date (text returned of (display dialog dialogMsg default answer ""))
	set optionsList to {"Seconds", "Minutes", "Hours", "Days", "Weeks", "Months"}
	set incrUnit to (choose from list optionsList with prompt "Choose interval...")
	if incrUnit is false then return
	set incrUnit to item 1 of incrUnit
	if incrUnit is "Seconds" then
		set incrMultiplier to 1 as integer
		set lcUnit to "seconds"
	else if incrUnit is "Minutes" then
		set incrMultiplier to 60 as integer
		set lcUnit to "minutes"
	else if incrUnit is "Hours" then
		set incrMultiplier to 3600 as integer
		set lcUnit to "hours"
	else if incrUnit is "Days" then
		set incrMultiplier to 86400 as integer
		set lcUnit to "days"
	else if incrUnit is "Weeks" then
		set incrMultiplier to 604800 as integer
		set lcUnit to "weeks"
	else if incrUnit is "Months" then
		set incrMultiplier to 1 as integer
		set lcUnit to "months"
	end if
	set dialogMsg2 to "Enter interval duration in " & lcUnit
	set incrAmount to text returned of (display dialog dialogMsg2 default answer "1")
	set newMultiplier to incrMultiplier * incrAmount
	--	display dialog "newMultiplier = " & newMultiplier
	changeCreationDate(fileList)
end open


on changeCreationDate(fileList)
	-- Idea from Daniel A. Shockley (http://www.danshockley.com) on macosxhints.com		
	repeat with oneItem in fileList
		
		if folder of (info for oneItem without size) is false then
			set shellCommand to "touch -t " & " " & quoted form of POSIX path of oneItem -- set to current date
			set creationDateString to year of newCreationDate as string
			set creationDateString to creationDateString & text -2 thru -1 of ("0" & getMonthNum(newCreationDate) as string)
			set creationDateString to creationDateString & text -2 thru -1 of ("0" & day of newCreationDate as string)
			set creationDateString to creationDateString & text -2 thru -1 of ("0" & hours of newCreationDate as string)
			set creationDateString to creationDateString & text -2 thru -1 of ("0" & minutes of newCreationDate as string)
			set creationDateString to creationDateString & "." & text -2 thru -1 of ("0" & seconds of newCreationDate as string)
			
			set shellCommand to "touch -t " & creationDateString & " " & quoted form of POSIX path of oneItem
			
			-- Add increments to date for next file in line
			set newMultiplier to integer newMultiplier
			if incrUnit is "Months" then
				set newCreationDate to newCreationDate + (newMultiplier * months)
			else
				set newCreationDate to newCreationDate + (newMultiplier * seconds)
			end if
			
			do shell script shellCommand
			
		end if
		
	end repeat
	
end changeCreationDate


on getMonthNum(theDate)
	-- From Nigel Garvey
	--set theDate to the current date --or any other date
	copy theDate to b
	set the month of b to January
	set monthNum to (theDate - b + 3944592) div 2629728
	return monthNum
end getMonthNum


Hi,

To #1: AppleScript has a few built-in constants for time calculating:

seconds = 1
minutes = 60
hours = 3600
days = 86400
weeks = 604800
(there is no constant for month, because it isn’t constant ;))

so you can write:

else if incrUnit is "Hours" then
       set incrMultiplier to hours
       set lcUnit to "hours"

doing math is “normal” e.g.
2 * minutes = 120

PS: the class of the result of “set whatever to 1” is integer, no coercion is needed

Thanks StefanK!

That was good help. For my second question there might be an alternative solution, but I’m stomped at how to accomplish that. If I could load my script in Automator AND allow my dialogues to appear I could have Automator take care of making sure that files are handled in the correct order. But how do I get the dialogs to appear in Automator?

I had it wrong, it’s the other way around. Getting the dialogs is easy, but getting the files in the right order (by name) is something I could use some help with.

The Finder has a sort command in it’s dictionary.
You can start your script with:

on open fileList
	tell application "Finder" to set fileList to sort fileList by name
	set dialogMsg to "Please enter new creation (DD MM YYYY):"
...

I get an odd error when I put that line in:

File <> image_020.jpg wasn’t found

The image_20.jpg is one of the files I try to open with this script. On repeat, it gives the same error with different filenames.

try this at the beginning of the handlerchangeCreationDate:

repeat with oneItem in fileList
       if folder of (info for (oneItem as alias) without size) is false then
...