• 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: Script Starter Templates
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script Starter Templates


  • Subject: Re: Script Starter Templates
  • From: Yvan KOENIG <email@hidden>
  • Date: Wed, 23 Dec 2015 21:06:35 +0100


Le 2015/12/23 à 19:08, S. J. Cunningham <email@hidden> a écrit :

I find myself needing to do the same thing in script after script so I have developed my own template as a "starter".

I wonder if anyone else has such a template they could share.

Here is mine:

# Script Debugger Template Version 2015-12-11

(*
Description of the code goes here.

To Do:
-- Item 1
*)

-- Properties to Make Script Relocatable.  ** NB: For this to work correctly in SD, script must be saved first.  Doesn't work in AppleScript Editor
property pMyHostName : host name of (system info) -- Host on which the script was compiled
property pMyScriptName : missing value
property pMyFolder : POSIX file (POSIX path of ((path to me as text) & "::")) as text
property pMyHomeFolder : path to home folder as text
property pConfigurationChange : true -- Flag a configuration change

-- Optional Log File
property pLogFile : missing value
property pLogFileName : missing value
property pLogFileFolder : "Library:Logs:"
property pLogDiagnosticInfo : true

-- Debug Control
property pDebugCaseTestLog : false -- Test writing to log even if app doesn't use log

-- Behavior Control
property pDialogTitle : missing value -- Run time. Holder for Display Dialog titles.  Default pScriptName
property pDialogTimeOut : 5 -- Optional time out for dialogs
property pFirstRun : true

-- Error Numbers
property pUserCanceled : -128 -- Display Dialog cancel button
property pFileAlreadyExists : -48 -- Finder error message

-- Miscellaneous Dictionary
property pReturn2 : return & return
property pContentsApplet : ":Contents:MacOS:applet" -- Specify the applet in the application bundle

-- Your Properties Go Here


on run
-------------------------------------------------------------------
-- Code to Make Script Relocatable
-------------------------------------------------------------------

-- First save the previous location values
set pConfigurationChange to false
if pMyScriptName is missing value then set pMyScriptName to getMyName() -- Must be the first run
set previousMyScriptName to pMyScriptName
set previousHostName to pMyHostName
set previousMyFolder to pMyFolder
set previousHomeFolder to pMyHomeFolder

-- Get the current location values
set pMyHostName to host name of (system info)
set pMyScriptName to getMyName()
set pMyFolder to POSIX file (POSIX path of ((path to me as text) & "::")) as text
set pMyHomeFolder to path to home folder as text

-- Flag if the configuration has changed
if previousMyScriptName ≠ pMyScriptName then set pConfigurationChange to true
if previousHostName ≠ pMyHostName then set pConfigurationChange to true
if previousMyFolder ≠ pMyFolder then set pConfigurationChange to true
if previousHomeFolder ≠ pMyHomeFolder then pConfigurationChange

-------------------------------------------------------------------
-- First Run Code
-------------------------------------------------------------------

if pFirstRun or pConfigurationChange then
set pLogFileName to pMyScriptName & ".log"
set pLogFile to POSIX path of (pMyHomeFolder & pLogFileFolder & pLogFileName)
set pDialogTitle to pMyScriptName
--  Your first run code goes here
set pFirstRun to false
set pConfigurationChange to false
else
-- Your code for every run but first run goes here
end if

-------------------------------------------------------------------
-- Every Run Code
-------------------------------------------------------------------
set handlerPrefix to camelCase(pMyScriptName)
if pDebugCaseTestLog then logEvent(handlerPrefix, "Log Test", pLogFile)

-- Your code for every run goes here

end run


-------------------------------------------------------------------
-- Your Script Handlers
-------------------------------------------------------------------


-------------------------------------------------------------------
-- Template Handlers
-------------------------------------------------------------------

on logEvent(theSender, theMessage, theLogFile)
-- theLogFile must be a POSIX path
if class of theLogFile is not text or the first character of theLogFile is not "/" then
display dialog "Error: theLogFile must be a POSIX path" with title "logEvent: " & theSender
return
end if

log (theMessage)

set theSender to quoted form of theSender
set theMessage to quoted form of theMessage
set theLogFile to quoted form of theLogFile
set theLine to (do shell script ¬
"date  +'%Y-%m-%d %H:%M:%S'" as string) & "  " & theSender & "  " & theMessage
log "theLine: " & theLine
try
do shell script "echo " & theLine & ¬
" >> " & theLogFile

on error errMsg number errNum
display dialog errMsg with title "logEvent: " & theSender
end try
end logEvent

on getMyName()
set x to path to me
tell application "Finder"
set y to name of file x as text
set z to "." & name extension of file x as text
end tell

set zz to offset of z in y
if zz = 0 then
set myName to y
else
set myName to (characters 1 thru (zz - 1)) of y as text
end if

return myName
end getMyName

on camelCase(aPhrase)
set camelCasePhrase to ""
repeat with nn from 1 to count of words of aPhrase
set theWord to word nn of aPhrase
set leadCharacter to first character of theWord
if nn = 1 then
if (ASCII number of leadCharacter) ≤ (ASCII number of "Z") then
set leadCharacter to ASCII character of ((ASCII number of leadCharacter) + 32)
end if
else
if (ASCII number of leadCharacter) > (ASCII number of "Z") then
set leadCharacter to ASCII character of ((ASCII number of leadCharacter) - 32)
end if
end if
set theWord to leadCharacter & (characters 2 through (count of theWord)) of theWord as text
set camelCasePhrase to camelCasePhrase & theWord as string
end repeat

return camelCasePhrase
end camelCase

#=====

on getTextItems(theText, replacementDelimiterList)
-- Uses Text Item Delimiters to extract text items.  Saves and restores existing delimiters
-- From Yvan Koenig
local existingDelimiterList, theTextItems
set {existingDelimiterList, AppleScript's text item delimiters} to {AppleScript's text item delimiters, replacementDelimiterList}
set l to text items of theText
set AppleScript's text item delimiters to existingDelimiterList
return theTextItems
end getTextItems

#=====

on makeTextFromItems(listOfItems, replacementDelimiterList)
-- Make a list of text items into text
-- From Yvan Koenig
local existingDelimiterList, theText
set {existingDelimiterList, AppleScript's text item delimiters} to {AppleScript's text item delimiters, replacementDelimiterList}
set theText to listOfItems as text
set AppleScript's text item delimiters to existingDelimiterList
return theText
end makeTextFromItems

#=====

on replaceText(theText, targetText, replacementText)
-- Replaces every occurence of targetText by replacementText in theText
-- From Yvan Koenig
local existingDelimiterList, intermediateText
set {existingDelimiterList, AppleScript's text item delimiters} to {AppleScript's text item delimiters, targetText}
set l to text items of theText
set AppleScript's text item delimiters to replacementText
set theText to intermediateText as text
set AppleScript's text item delimiters to existingDelimiterList
return theText
end replaceText

#=====

Hello Steve

(1) Your camelCase handler maybe fine for English users - although I dislike the fact that it use the deprecated ASCII number.
For non English users, it fail to convert the accented chars.

Here is a version which may be used everywhere.
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set newPhrase to its camelCase:"un éléphant ça trompe, ça trompe. un éléphant ça trompe énormément"
--> "Un Éléphant Ça Trompe, Ça Trompe. Un Éléphant Ça Trompe Énormément"

on camelCase:aPhrase
set listOfWords to my getTextItems(aPhrase, space)
set thisLocale to current application's NSLocale's currentLocale()
repeat with i from 1 to count of listOfWords
set begWord to (current application's NSString's stringWithString:(text 1 of (item i of listOfWords)))
set restWord to (current application's NSString's stringWithString:(text 2 thru -1 of (item i of listOfWords)))
set begUp to (begWord's uppercaseStringWithLocale:thisLocale) as text
set restLow to (restWord's lowercaseStringWithLocale:thisLocale) as text
set item i of listOfWords to begUp & restLow
end repeat
return my makeTextFromItems(listOfWords, space)
end camelCase:

#=====

on getTextItems(theText, replacementDelimiterList)
-- Uses Text Item Delimiters to extract text items.  Saves and restores existing delimiters
-- From Yvan Koenig
local existingDelimiterList, theTextItems
set {existingDelimiterList, AppleScript's text item delimiters} to {AppleScript's text item delimiters, replacementDelimiterList}
set theTextItems to text items of theText # EDITED
set AppleScript's text item delimiters to existingDelimiterList
return theTextItems
end getTextItems

#=====

on makeTextFromItems(listOfItems, replacementDelimiterList)
-- Make a list of text items into text
-- From Yvan Koenig
local existingDelimiterList, theText
set {existingDelimiterList, AppleScript's text item delimiters} to {AppleScript's text item delimiters, replacementDelimiterList}
set theText to listOfItems as text
set AppleScript's text item delimiters to existingDelimiterList
return theText
end makeTextFromItems

(2) when yo changed its name and the names of the used variables in my decoupe(t, l) handler, you changed a name once although it is used twice.
Above, in the handler getTextItems the error is corrected.

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) mercredi 23 décembre 2015 20:55:56



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Script Starter Templates
      • From: Shane Stanley <email@hidden>
References: 
 >Script Starter Templates (From: "S. J. Cunningham" <email@hidden>)

  • Prev by Date: Re: Script Starter Templates
  • Next by Date: Re: AppleScript-Users Digest, Vol 12, Issue 643
  • Previous by thread: Re: Script Starter Templates
  • Next by thread: Re: Script Starter Templates
  • Index(es):
    • Date
    • Thread