saving file title with date

I’m using a save as pdf command to save the name with text and “ct” which is the date.
I’m having a problem with the time it saves. When I look at “confirmationof” I get 7:00:00 and when I look at the file name that was saved I get 7-00-00 which gives me an error. Why is it saving different ways and how can I get both the alias and saved file to have colons?

click menu item "Save as PDF." of menu 1 of menu button "PDF" of sheet 1 of window 1
	delay myDelay
	keystroke confirmationo & ct
	click button "Save" of window "Save"
end tell

tell application "Safari"
	set theURL to the URL of the front document
end tell
set confirmationof to (path to desktop folder as text) & confirmationo & ct & ".pdf"
alias confirmationof

I have run across this “type a save name with a colon, get a filename that uses some other character” problem before. I would guess that it has to do with the fact that the colon is the path component separator for HFS pathnames. That implies that it cannot be part of a filename.

Since having an (HFS) filename with a colon in it is “impossible”, I would advise using a different character in your filename. Once you are no longer using invalid characters your alias construction code should work fine (assuming that “Save as PDF.” defaults to the desktop folder). This assumes that you could avoid all possible problematic characters. If you assume that the desktop folder is on an HFS+ filesystem (which is a pretty safe assumption), then the colon should be the only character you have to worry about.

Alternatively you could try doing the dash-for-colon replacement on the filename in your script and creating your alias object from that modified filename. This alternative makes the assumption that all applications (and application contexts) will make this substitution in the same way (maybe some other app uses equal sign instead of dash; I do not know if this is reasonable thing to worry about since I am unsure of the origin of this replacement scheme).

The core problem stems from the fact that you are using UI-scripting. A user facing this last minute change in filename would take it mostly in stride because users employ “fuzzy” tools like vision to scan for the new file instead of relying exclusively on the originally supplied filename (as your script tries to do). If the user ever noticed that the colons had been changed to dashes they would most likely shrug it off and use the file (click on it, etc.) just as if it had been given its originally specified name.

Unfortunately scripts are not usually quite as adaptable as users. One way to try to make the script more flexible would be to try to approximate what a human user would do.

You could have the script ask the Finder for all PDF files in the desktop folder that were created after a date just before the “Save” button was clicked. That would probably work OK in most cases, but there are still potential problems.

What if a background download of a PDF file finished right after the “Save” was clicked? How can the script differentiate between this downloaded file and the “printed” file? One way might be to use the prefix or some substring of the originally specified filename as an indicator. If the filename has this indicator it probably is the printed file, not the downloaded file. Such a substring should be designed/selected based on universality (it should not have colons or other characters that some filesystems might not accept) and uniqueness (it should not be so common that, for example, a randomly downloaded PDF file would also have the same indicator).

Or if multiple potential files were found the script could prompt the user or just abort.