Cool Date Stamper but Random Unexpected Behavior...

I use this little date stamper script I compiled for my note taking that I activate with a hotkey. It works pretty good most of the time but I get some unexpected results occasionally. Is there a flaw is my script?

Here’s a list of the strange results that I get sometimes…

–The date stamped without the proper formatting
–The wrong time stamped like it is pasting an old stamp
–Pasting something else from my clipboard that is not a time stamp at all
–A long delay or where it never stamps at all upon command

Any thoughts?


to makeStamp(Now, DelimD, DelimT) -- the first delim for date, the second for time.
	tell Now to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
		to set dateStamp to text -2 thru -1 & DelimD & text 4 thru 5 & DelimD & text 2 thru 3
	tell Now to tell ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as string) ¬
		to set timeStamp to text 2 thru 3 & DelimT & text 4 thru 5 & DelimT & text 6 thru 7
	return dateStamp & " " & timeStamp
end makeStamp

set the clipboard to makeStamp((current date), "/", ":")
set the clipboard to (do shell script "date -n +'**%m-%d-%y@%H:%M** - '")
tell frontmost to activate
tell application "System Events"
	keystroke "v" using command down
	
end tell

Thank you in advance for your help.

Curtis

Model: MacBook Pro
AppleScript: 2.4 (128)
Browser: Firefox 5.0.1
Operating System: Mac OS X (10.6)

I have no idea why your script errors now & then - maybe a timing issue.
You have two ‘set clipboard’ statements; the result you get is from the 2nd one.
The other is just wasting CPU time (and possibly causing the problem).

Here’s a simple way to get (almost) the same result.
It adapts to any system’s date format.

set this to current date
set the clipboard to short date string of this & "@" & time string of this & "** - "

tell frontmost to activate
tell application "System Events"
	keystroke "v" using command down
end tell

Go to System Preferences>Language & Text to customise date/time formats.

That is certainly shorter and simpler. I have been using it and it works about the same but a little faster.

Do you have any idea why it would past sometimes in ALL CAPS and sometimes not. It also pastes sometimes the clipboard contents that preexisted and sometimes the previous time from several minutes past.

It is almost like the first part of the script is not always successfully setting the clipboard contents.

A bit weird.

Thank you so much already.

Curtis

P.S. Doesn’t seem to be timing or in my mind it would at least get the paste right the second time.

Here is an example of what is does when I just do it every five seconds…

Sat - 03.Sep.11 @ 5:02:03PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:02:16PM - Sat - 03.Sep.11 @ 5:04:05PM - Sat - 03.Sep.11 @ 5:04:05PM - Sat - 03.Sep.11 @ 5:04:05PM - Sat - 03.Sep.11 @ 5:04:05PM - Sat - 03.Sep.11 @ 5:04:05PM - Sat - 03.Sep.11 @ 5:04:05PM -

Ok, that wasn’t exactly every 5 seconds but you get the point. Any ideas?

I remembered writing a service to do the same thing.
It does not use the clipboard at all.
Reshuffled to line up with what I posted:

set this to current date
set dateStamp to short date string of this & "@" & time string of this & "** - "

tell frontmost to activate
tell application "System Events"
	keystroke dateStamp
end tell

And it’s faster again, compared to using the clipboard.

I still think there’s something fishy about the clipboard…

Thank you so much… Definitely some fishyness.Here is exactly what I am using and what is happening. Do I need to do something special to set up a “service”?

set this to current date
set dateStamp to "**" & date string of this & " @ " & time string of this & "** - "

tell frontmost to activate
tell application "System Events"
	keystroke dateStamp
end tell

Here is what I have tried so far.

  1. Simply create a script with the above and using Alfred assign a hotkey to activate a script.

The first attempt I get this Sun-04.Sep.11@11:38:04AM-

Then,

ÜÉͲ¹¹ <— There is the output but it also opens Skype, Address Book, Preferences, and Mail.

Then the third time, I lose typing capability and have to restart.

This is not hotkey related because it only happen with this new script. If I attach the other script to the same hotkey I get the previously described results.

  1. If I put the script in Automator as a “Service” and assign a hotkey via the keyboard preference pane then I simply get no result at all.

I am a bit confused to say the least.

Curtis

P.S. The help is greatly appreciated.

P.S.S. You will also notice that the “proper” output also for some reason loses the spacing.

Mmyes. It doesn’t too well here, either. I only ran it from the Editor.
I changed it to use a 'full incantation":

set this to current date
set dateStamp to "**" & date string of this & " @ " & time string of this & "** - "

tell application "System Events"
	set theName to name of the first process whose frontmost is true
end tell

tell application theName
	activate
	tell application "System Events"
		tell process theName
			keystroke dateStamp
		end tell
	end tell
end tell

Saved it as script in my Scripts folder, and tested with Text Editor & Mail. Works.
To always have it available in the Script Menu I have a folder ~/Library/Scripts/Utilities/, where I put scripts such as this one. I’m using that now, because services took forever to kick in.

Having a bunch of apps open at once is weird indeed. Somewhere between “frontmost” and Alfred things seem to go where they shouldn’t. I’m stumped.

I am confused also. Totally weird. Same thing is happening. For now I am using this one…

set this to current date
set the clipboard to "**" & date string of this & " @ " & time string of this & "** - "

I reset the hotkey to option-c then immediatly hit command v and it does what I want. Though not as slick.

This is so strange.

Sun - 04.Sep.11 @ 3:44:12PM - Sun - 04.Sep.11 @ 3:44:23PM - Sun - 04.Sep.11 @ 3:44:27PM -

But at least it works.

I am stumped too.

Curtis