Re: "do shell" vs. Plain old AppleScript nomenclature
Re: "do shell" vs. Plain old AppleScript nomenclature
- Subject: Re: "do shell" vs. Plain old AppleScript nomenclature
- From: Graff <email@hidden>
- Date: Fri, 23 Jul 2004 13:22:26 -0400
Well first of all you can get a time string very simply just using
AppleScript:
time string of (current date)
-->"12:54:32 PM"
As for the "date" shell tool if you do a "man date" you will see the
following text in the man page:
"The format string may contain any of the conversion specifications
described in the strftime(3) manual page, as well as any arbitrary
text."
So you should then do a "man strftime" and you'll get all the
specifications for the format string. For example, for a time stamp
that looks like 4:23 PM you would do:
date +'%l:%M %p'
Here is also a little bit cleaner version of your script. It's
basically what you wrote but I used some of the time constants to make
it a bit more readable. You probably should use the set command rather
than the copy command when you want to set a variable. It's more
readable and it uses less resources to set something rather than copy
it:
----
on padNum(theNumber)
if (theNumber < 10) then
return "0" & theNumber
else
return "" & theNumber
end if
end padNum
set theDate to current date
set theTime to (time of theDate)
set theHour to (theTime mod days) div hours
set theMinute to (theTime mod hours) div minutes
set theSeconds to theTime mod minutes
if theHour < 12 then
set amPM to "AM"
else
set amPM to "PM"
set theHour to theHour - 12
end if
display dialog padNum(theHour) & ":" & padNum(theMinute) & ":" &
padNum(theSeconds) & " " & amPM
----
- Ken
On Jul 23, 2004, at 11:58 AM, D. Mahaffy wrote:
I'm new to this list, and thankful that it exists! There are some
serious experts out there! :-)
That said, I'm looking for an answer/explanation regarding AppleScript
and the "do shell script" command.
Simply, why would I want to use "do shell script" as opposed to parsing
out my request using AppleScript "lingo"? (Pardon my nomenclature, I'm
a "novice" when it comes to scripting).
I've got a script that parses out the date and time that is "long" for
what I'm needing (doing time-zone compensation, etc.). It seems that
the main (positive) thing with shell scripting via A/S is brevity.
But another poster (Andrew Oliver) said, essentially, why spawn extra
shell process when you don't need to?
Here's a sample of getting time and date:
set theDate to current date
set theTime to (time of theDate)
copy (round (theTime / 3600) rounding down) to theHour
copy (round ((theTime - (theHour * 3600)) / 60) rounding down) to
theMinute
copy ((theTime - (theHour * 3600)) - theMinute * 60) to theSeconds
if theHour < 12 then
copy "AM" to amPM
else
copy "PM" to amPM
if theHour > 12 then
copy theHour - 12 to theHour
end if
end if
if theMinute < 10 then
copy "0" & theMinute to theMinute
end if
With "do shell script" I gather that some of this can be shortened
using the % commands, yes? Is there a reference of the commands for
time/date - and HOW to write them? I find the script library commands,
while useful for finding commands, is not useful for how to properly
write them out (for newbies, at least).
Did find shell references on the web, but experimented with and without
success. Date parsing was somewhat successful, but getting a time
stamp of say: 4:23 PM was not. Oh, and I take away an hour since I am
EDT and the web site is CDT. So that comes in later. But if someone
knows how to do it more easily!
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.