I am in the midst of a small project. I have looked through the three posts in this forum concerning dates and have come up with a little more clarity but I still need help.
Question #1
I am trying to display the date & time of a different timezone. I already have a shell script that accomplishes this but I am wondering if there is a smoother cooler AppleScriptObjC way of doing this.
Here is the shell script that I have:
on awakeFromNib()
set contactTime to do shell script "TZ=America/New_York date +'%I:%M %p on %A'"
theField's setStringValue_("His current time is " & contactTime & ".")
end awakeFromNib
Is there cleaner or better way to do this? I have noticed that the application doesn’t always launch very quickly and from posts on other applescript forums some more experienced posters seem to discourage other users from using shell scripts in applescript.
Question #2
I have found this in one of the other posts in this forum and this will work to display the current date and time. However it displays a lot of information I am not looking for. I really want something simple and easy to read like “09:05 PM on Thursday”.
on awakeFromNib()
set myDate to current application's NSDate's |date|()
theField's setStringValue_(myDate)
end awakeFromNib
There’s nothing wrong with using shell scripts, but often you can get something closer to what you want using Cocoa.
What you’re after is NSDateFormatter. If you’ve updated to Xcode 3.2.2 with the latest document updates, look up setDoesRelativeDateFormatting: for some sample Objective-C code you can probably translate.
using a NSDate object as a parameter for a string value sends always the method description to the object.
Description returns normally a string with format YYYY-MM-DD HH:MM:SS ±HHMM (±HHMM represents the time zone offset)
The easiest way to get a custom string from a date is the method descriptionWithCalendarFormat:timeZone:locale:, which can include also time zone and local date format information
Your example “09:05 PM on Thursday” can be created in Cocoa with
Thank you both for posting a reply, I appreciate it. I have spent the last couple hours trying to figure out how to convert what I have found in your posts and the developer documentation into use but I am at a loss. I have tried reading over Craig’s tutorial #5 again trying to pick up some more information on using the developer documentation but I am still confused and frustrated. I would love to do this on my own but I can’t. Can someone break it down for me in simple words? I am lost on converting Objective C to Applescript ObjC.
Hi,
I’m not sure quite what you’re trying to do, but if you want to set a date in a different time zone relative to your current time, you can do that with standard applescript language. For example, if you were in the central time zone you could do something like this:
script DatesAppDelegate
property parent : class “NSObject”
property dateField : missing value --attached to a label in the nib
property est : 1
property cst : 0 --these values assume you are in the central standard time zone
property mst : -1
property pst : -2
on applicationWillFinishLaunching_(aNotification)
set now to the (current date) + (est * hours)
set timePart to (time string of now)
set dayPart to weekday of now
dateField's setStringValue_(timePart & " on " & dayPart)
end applicationWillFinishLaunching_
Thanks for your reply. I appreciate it. Here are some more details of my project. I have written a small application for each of my close friends and family. The application interacts with Skype and Mail. There is some variation in the button assignments, but generally here is how I have them set up:
Button 1:
The first button triggers a phone call to their home number.
Button 2:
The second button triggers a phone call to their cell phone or skype account.
Button 3:
The third button triggers an email to their cell phone providers sms gateway. (their cell phones email address)
Button 4:
The fourth button triggers an email to their main email account.
Each of the contacts applications have their picture as the icon, so I can just click on their picture to launch the application. Thanks to previous posters on my question a couple weeks ago, key equivalents have been defined, so I can just type one letter to click a button.
To the left of the buttons I have a static image of the contact. The first label displays the contact’s current time. Most of my family and close friends are scattered between, west coast & east coast of america, and a couple throughout Asia. Some of the timezones don’t observe daylight savings times.
I already have the timezone information working with a shell script. I was just wondering if there was a faster way to do it with applescript objc.
Hi,
So, if I’m understanding correctly, you have several applications but each one just brings up one contact and shows their time with several buttons to choose a way to contact them. If that’s so, then you can use a simpler version of what I posted before:
property timeDiff:0
on applicationWillFinishLaunching_(aNotification)
set now to the (current date) + (timeDiff * hours)
set timePart to (time string of now)
set dayPart to weekday of now
dateField's setStringValue_(timePart & " on " & dayPart)
end applicationWillFinishLaunching_
Then you just need to replace the “0” in the property timeDiff with whatever you know to be the difference in hours between your time and theirs (negative numbers for those to your west and positive to the east for the US. But for asia, because of the international dateline you need to use positive numbers. For instance, from the pacific time zone to Japan’s timezone timeDiff would be 16).
Thanks for getting back to me so soon. I really appreciate your help. The problem is that I live in Asia, and we do not observe daylight savings time, and many of my contacts do. So each time they (my contacts) spring ahead or fall back, I would have to edit each of the scripts because the time differences will have changed.
Does that make sense?
I think I found something that will work. Try this:
script TimeZonesAppDelegate
property parent : class “NSObject”
property NSTimeZone : class "NSTimeZone"
property NSDate : class "NSDate"
property dateField : missing value --connected to a label in the nib
on applicationWillFinishLaunching_(aNotification)
--set myList to NSTimeZone's knownTimeZoneNames()
--log myList
set aZone to NSTimeZone's timeZoneWithName_("America/New_York")
NSTimeZone's setDefaultTimeZone_(aZone)
set now to NSDate's |date|
dateField's setObjectValue_(now)
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
If you uncomment the first 2 lines in applicationWillFinishLaunching and run the program with the console window open it will give you a list of all the timezone names around the world. Just substitute the correct one of those for “America/New_York” and I think it will work. The setDefaultTimeZone method make your applescript (but not your computer) think that it’s in the timezone that you supply. I think it will automatically take care of the daylight savings time changes, but I can’t be sure of that.
You will get a long format date from this like: Saturday, April 10, 2010 2:12:47 AM ET
Unfortunately, trying to use a date formatter to parse this to something shorter didn’t work properly for some reason.
-- get date
set theNSDate to current application's NSDate's |date|()
-- pick locale
set myLocale to current application's NSLocale's alloc()'s initWithLocaleIdentifier_("fr_FR") -- whatever
-- make formatter
set theFormat to current application's NSDateFormatter's alloc()'s init()
-- set up formatter
tell theFormat
setLocale_(myLocale)
setDoesRelativeDateFormatting_(true)
setTimeStyle_(current application's NSDateFormatterShortStyle)
setDateStyle_(current application's NSDateFormatterLongStyle)
-- produce string from date
set theString to stringFromDate_(theNSDate)
log theString
end tell
Shane, do you know if there is a known bug in the date formatter in IB? Using the code I posted above, I get the correct date and time for the time zone that I set when there is no date formatter attached to the label, but if I attach a date formatter and use the 10.4+ setting I get my computer’s correct time – it seems to ignore the date object that I send it. However, if I use the 10.0+ setting, I get the correct date and time for the specified time zone, and I can manipulate the format however I want.
Confused,
You can try Shane’s method for doing this in code, or you can do the formatting in Interface Builder.
You can get the output you want by attaching a date formatter to your text field (or label) and use the “Mac OS X 10.0+” setting for Behavior in the attributes inspector. Choose Basic for the Edit Mode and then you can delete what shows up in the Format window by clicking on them and pressing delete. Then you can drag in the blue oval next to hour to put the hour digits in (you can click on the little triangle to see options such as whether you will get 8 or 08). After you drag something in you can put your cursor to the right of the oval and type in whatever you want such as a colon, a space or any text you want. This way you can get something like “10:15 AM on Saturday” if that is what you want.
Thank you guys all so much for your help. I went ahead and used the code that rdelmar provided. I had not known about date formatters before! I was so happy you were able to teach me about that. Here is the final code I went with.
script TimeZoneAppDelegate
property parent : class "NSObject"
property NSTimeZone : class "NSTimeZone"
property NSDate : class "NSDate"
property dateField : missing value --connected to a label in the nib
on applicationWillFinishLaunching_(aNotification)
--set myList to NSTimeZone's knownTimeZoneNames()
--log myList
set aZone to NSTimeZone's timeZoneWithName_("Asia/Tokyo")
NSTimeZone's setDefaultTimeZone_(aZone)
set now to NSDate's |date|
dateField's setObjectValue_(now)
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
One last question, what is the difference between “on applicationWillFinish launching” and “on awakeFromNib()”?
I’m not sure what you’re asking for here. Do you want to take the UTC formatted string and turn it into something else? From my experimentation, the date formatter’s don’t work with that as an input. Why not just start with the result you get from NSDate’s |date|() ?