![]() ![]() Notice what the compiler does to your string when you compile. Given a string in a format that it understands, AppleScript can convert a written (string) date into a date object. On the other hand (depending on the settings in System Preferences in the computer’s Language & Region Panel), the following may give you just what you want for a dateStamp: set shortDate to short date string of (current date) FROM STRING TO DATE Set theDay to text -1 thru -2 of ("0" & day of dateObj) Set theMonth to text -1 thru -2 of ("0" & (month of dateObj as number)) Substituting this in the Date Stamp script above, the result will always have two digits for the day and the month. Now try the above with the value of 15 for the value of theDay. Set dayStr to text -1 thru -2 of ("0" & theDay) See how it works with the following simple test: set theDay to 5 The following, however, is a way this can be done with string concatenation. To make sure that the value for the day and month is always a string having two digits (a zero for the first if less than 10), you could have the script test to see if the value for the day and month are less than 10 and add a zero as needed. The problem is that the value for theDay and theMonth in the above can be one or two digits. Set dateStamp to "" & theMonth & theDay & theYear Set theMonth to month of dateObj as number With the month constant returned as a number you might think you could use the following to create a date stamp in the format MMDDYYYY set dateObj to (current date) Similarly the weekday can be returned as a number (in versions from Panther up): set wDay to weekday of (current date) as number Date Stamp (You still might need to use the old repeat with/exit repeat/end repeat procedure if you are not sure the system that the script will be running on.) ![]() (Don’t forget to place the variable i at the end to get the result for the month.) Repeat with i from 1 to length of monthList ![]() Prior to Panther, a script would have to repeat through a list of months to get the index of the month from the list: set monthList to Instead of the name of the month, you now get the index of the month. Set theResult to month of dateObj as number This is only possible in later versions of AppleScript (added in Panther). The constants for month and weekday can now be coerced to a number. Suppose you want a script to show the date as numbers instead of text. On the other hand (depending on the settings for the computer’s System Preferences Language & Region Panel), the following may give you just what is needed for your date string: set dayStr to date string of (current date) Date Stamp Set dateStr to monthStr & " " & theDay & ", " & theYear What’s the Date? set dateObj to (current date) Using this, we can put the results together (concatenate) to get today’s date in the format needed. Notice that the result (monthStr variable) is the name of the month now with quotation marks. Set monthStr to month of dateObj as string ![]() Try the following: set dateObj to (current date) These constants can be coerced into strings as needed. Notice there are no quotes around the month and weekday results. The value returned for the month and weekday, although they appear to be strings (literal text), are constants. For the second statement in the above, substitute each of the following and test. Similarly, you can get the weekday, date and year for the dateObject. For this reason, the date object has a number of properties which can be used to format the string value for the date object. The way the date elements are arranged in the string may or may not be what is wanted for the purpose of the script. How the date string is formatted depends on the settings for the computer’s System Preferences (in the Language & Region Panel). Note: If you don’t put the parentheses around current date, the script compiler will gladly add them for you when you compile (Command+K). To get the string value of the date object, coerce it to a string: (current date) as string The result, a date object, will display in the Result window at the bottom. For example, in the editor enter the following and run the script (Command+R) current date New to AppleScript? The AppleScript Editor allows you to see the result of a procedure in its Result window. Make sure you remove these characters and make the statement one line. NOTE: In some of the sample scripts we have used the line return character (¬) to shorten some extra long statements into two lines. But “those in the know” may also find some new information tucked away in the examples below. This post is designed for new users who may be struggling when it comes to working with AppleScript’s date object. With the New Year, thoughts may tend to turn to dates that is, days of the calendar. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |