• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: "do shell" vs. Plain old AppleScript nomenclature
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "do shell" vs. Plain old AppleScript nomenclature


  • Subject: Re: "do shell" vs. Plain old AppleScript nomenclature
  • From: "D. Mahaffy" <email@hidden>
  • Date: Mon, 26 Jul 2004 10:17:01 -0400

I don't think my reply went through previously (on the 24th) ... so
here it is again.
--
Best,
Darren
MacSurfer

On Jul 24, 2004, at 11:02 AM, Darren Mahaffy wrote:

> A hearty thanks!! to all who've responded! Some excellent suggestions.
>
> What I've done is combine some of LuKreme's script with some of Ken's
> re-do of my script.
>
> The overall result is a shorter script and seems quite fast. Here's
> the snippet (and of course, if it can be even better, then by all
> means!)
>
> --Get Time and Date and break into variables
> set theDate to current date
> set theTime to (time of theDate)
> set theMonth to do shell script "date +%m"
> set theDay to day of (current date)
>
> set HourMin to do shell script "date +%I%M%p"
> set theHour to text 1 through 2 of HourMin as string
> set theMinute to text 3 through 4 of HourMin as string
> set amPM to text 5 through 6 of HourMin as string
>
> if first character of theHour is equal to "0" then
> set theHour to character 2 of theHour
> end if
>
> if first character of theMonth is equal to "0" then
> set theMonth to character 2 of theMonth
> end if
>
> display dialog "" & theHour & ":" & theMinute & " " & amPM
> ( I only use the display dialog for testing - I "set selection" in
> BBEdit, normally)
>
> === truncated script ===
>
> if theHour is "12" and amPM is "PM" then
> set t to ("11" & ":" & theMinute & " " & "AM") as text
> else
> if theHour is "12" and amPM is "AM" then
> set t to ("11" & ":" & theMinute & " " & "PM") as text
> else
> if theHour is "1" and amPM is "PM" then
> set t to ("12" & ":" & theMinute & " " & amPM) as text
> else
> --NORMAL Time Stamp - Eastern Time-Zone to Central Time-Zone
> set t to (theHour - 1 & ":" & theMinute & " " & amPM) as text
>
>
>
> It was "shorter" to do shell script for the hour and minute, at least
> in my estimation. If I'm incorrect in that assessment, please
> indicate why.
>
> I get the month and all via shell script for breaking down date stamps
> for weekends and holidays, i.e., "7/24". It was shorter than the
> applescript way, as well, as I had to break down the month into
> numeric variables to get "7/24".
>
> Instead of this:
> repeat with theMonth from 1 to 12
> if month of (current date) = item theMonth of ,
> {January, February, March, April, May, June, July, August,
> September, October, November, December} then exit repeat
> end repeat
> copy item theMonth of {"1", "2", "3", "4", "5", "6", "7", " 8", "9",
> "10", "11", "12"} to theMonth
>
> I can use this:
> set theMonth to do shell script "date +%m"
> if first character of theMonth is equal to "0" then
> set theMonth to character 2 of theMonth
> end if
>
> It seems that the less-verbose way would be more efficient, yes?
>
> For the second part of the (=== truncated script ===) where I have to
> fix a couple of anomalies for EDT -> CDT time-stamping, I'm wondering
> if there's a better way to do it? Is there a setting I can set to
> automatically set theHour back an hour w/o having to break it down as
> above? Obviously on the hours above it isn't just theHour than has to
> be reset, the amPM does as well.
>
> Another question - is it truly necessary to put "as text" at the end
> of the "set t to (theHour blah blah) as text"? I notice some do and
> some do not. Curious as to the why of it.
>
> Thanks!
> --
> Best,
> Darren
> MacSurfer
>
> On Jul 23, 2004, at 10:15 PM, LuKreme wrote:
>
>> On 23 Jul 2004, at 09:58, D. Mahaffy wrote:
>>> 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).
>>
>> For the same reason you might want to use a hex wrench on a screw
>> instead of a flathead. Right tool for the job and all that.
>>
>>> 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?
>>
>> Depends on if spawning the shell script will save you time and make
>> your script faster.
>>
>>> Here's a sample of getting time and date:
>>
>> -- %I is 0 padded 12 hour clock, %M is minutes and %p is AM/PM
>> set HourMin to do shell script "date +%I%M%p"
>> set theHour to text 1 through 2 of HourMin as string
>> set theMinute to text 3 through 4 of HourMin as string
>> set amPM to text 5 through 6 of HourMin as string
>>
>> -- We use the 0 pad for parsing purpses, but we don't want the 0
>> if first character of theHour is equal to "0" then
>> set theHour to character 2 of theHour
>> end if
>>
>> set theTime to (theHour & ":" & theMinute & " " & amPM) as text
>> display dialog theTime
>>
>>> 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).
>>
>> `man date` will lead to `man strftime`
>>
>>
>>> 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.
>>
>> date "+%l:%M %p"
>>
>> --
>> Behind every great man there's a woman with a vibrator -- Hawkeye
>> Pierce
>>
>> [demime 0.98b removed an attachment of type
>> application/pkcs7-signature which had a name of smime.p7s]
>> _______________________________________________
>> 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.
_______________________________________________
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.


  • Follow-Ups:
    • Re: "do shell" vs. Plain old AppleScript nomenclature
      • From: Graff <email@hidden>
    • Re: "do shell" vs. Plain old AppleScript nomenclature
      • From: Nigel Smith <email@hidden>
References: 
 >set computer name (From: Andre Vink <email@hidden>)
 >"do shell" vs. Plain old AppleScript nomenclature (From: "D. Mahaffy" <email@hidden>)
 >Re: "do shell" vs. Plain old AppleScript nomenclature (From: LuKreme <email@hidden>)

  • Prev by Date: Re: Safari adds 2 windows (partially solved)
  • Next by Date: Photoshop resize image
  • Previous by thread: Re: "do shell" vs. Plain old AppleScript nomenclature
  • Next by thread: Re: "do shell" vs. Plain old AppleScript nomenclature
  • Index(es):
    • Date
    • Thread