Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* =================== Subroutines ================== *)
- -- Required by debug routine.
- global debugRunning
- set debugRunning to ""
- -- ------------------------------------------------------
- (*
- alterString
- thisText is the input string to change
- delim is what string to change. It doesn't have to be a single character.
- replacement is the new string
- returns the changed string.
- *)
- on alterString(thisText, delim, replacement)
- set resultList to {}
- set {tid, my text item delimiters} to {my text item delimiters, delim}
- try
- set resultList to every text item of thisText
- set text item delimiters to replacement
- set resultString to resultList as string
- set my text item delimiters to tid
- on error
- set my text item delimiters to tid
- end try
- return resultString
- end alterString
- -- ------------------------------------------------------
- (*
- *)
- on appendToFile(fileId, theData)
- local theSize, writeWhere
- set theSize to (get eof fileId)
- set writeWhere to theSize + 1 as integer
- write theData to fileId starting at writeWhere
- end appendToFile
- -- ------------------------------------------------------
- (*
- debug(<string>)
- Write messages to a log file.
- -- Need to place these two lines in the calling routine.
- global debugRunning
- set debugRunning to ""
- -- references appendToFile()
- -- example:
- debug("start program. Reading from " & listOfFiles)
- found here: /Users/mac/Documents/BJ\ Prior\ Years/BJ2004/sendmailapp2\ copy
- *)
- on debug(theMessage)
- -- return
- global debugRunning
- local theSize, startupDiskName, pathToLog, fileReference
- set pathToLog to (path to home folder as text) & "tryAttachmentsLog.txt"
- -- log "pathToLog is " & pathToLog
- -- display dialog "pathToLog is " & pathToLog giving up after 4
- try
- -- Complete the path.
- set pathToLog to pathToLog as text
- set fileReference to (open for access file pathToLog ¬
- with write permission)
- if debugRunning = "" then
- set theSize to (get eof fileReference)
- if theSize > 0 then
- appendToFile(fileReference, " " & return)
- end if
- appendToFile(fileReference, " --- debug on " & ((current date) as string) & " --- " & return)
- set debugRunning to "running"
- end if
- -- log "theMessage " & theMessage
- -- display dialog "in debug..." & return & "theMessage " & theMessage giving up after 3
- appendToFile(fileReference, theMessage & return)
- close access fileReference
- tell application "Finder"
- set the creator type of the file pathToLog ¬
- to "R*ch"
- end tell
- on error mes number n
- try
- set commonErr to "error ... " & mes & " error number is " & n
- log commonErr
- close access fileReference
- display dialog commonErr giving up after 4
- end try
- end try
- -- log "end of debug"
- end debug
- -- ------------------------------------------------------
- (*
- write log message to script editor log and to our file log
- *)
- on debugLog(theMessage)
- log "debugLog: " & theMessage
- return debug(theMessage)
- end debugLog
- -- ------------------------------------------------------
- (*
- A sub-routine for encoding ASCII characters.
- encode_char("$")
- --> returns: "%24"
- based on:
- https://www.macosxautomation.com/applescript/sbrt/sbrt-08.html
- alternative way
- https://stackoverflow.com/questions/11085654/apple-script-how-can-i-copy-html-content-to-the-clipboard
- set clipboardDataQuoted to quoted form of clipboardData
- log "quoted form is " & clipboardDataQuoted
- set toUnix to "/bin/echo -n " & clipboardDataQuoted & " | hexdump -ve '1/1 \"%.2x\"'"
- *)
- on encodeCharactersInHex(theCharacters)
- set charactersTranslated to ""
- repeat with i from 1 to length of theCharacters
- set this_char to text (i) thru i of theCharacters
- set the ASCII_num to (the ASCII number this_char)
- set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
- set x to item ((ASCII_num div 16) + 1) of the hex_list
- set y to item ((ASCII_num mod 16) + 1) of the hex_list
- set charactersTranslated to charactersTranslated & (x & y) as string
- end repeat
- return charactersTranslated
- end encodeCharactersInHex
- -- ------------------------------------------------------
- (*
- http://krypted.com/mac-os-x/to-hex-and-back/
- *)
- on hexToString(hex)
- log "in hexToString"
- log "hex string is " & hex
- set toUnix to "echo " & hex & " | xxd -r -p "
- log "toUnix is " & toUnix
- try
- set fromUnix to do shell script toUnix
- log "fromUnix is " & fromUnix
- on error errMsg number n
- log "convert hex string to string failed. " & errMsg & " with number " & n
- end try
- end hexToString
- -- ------------------------------------------------------
- (*
- ideas from:
- https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
- use the alias way. Others ways ran into trouble eventually.
- *)
- on fileExists(theFile) -- (String) as Boolean
- (* "System Events" and "Finder" checking for file existance revealed problems. l*)
- set debugging to false
- if debugging then log " fileExists: theFile is " & theFile
- try
- set theAlias to theFile as alias
- set theExistance to true
- on error errMsg number n
- if debugging then log " fileExists: n is " & n
- -- File or folder doesn't exist.
- if n is not -43 then
- set commonError to "on error the errMsg is " & errMsg & " number is " & n
- if debugging then log " fileExists: " & commonError
- display dialog commonError giving up after 10
- -- cause grief above.
- error "Failure of alias." number -1
- else
- set theExistance to false
- end if
- end try
- if debugging then log " fileExists: theExistance is " & theExistance
- return theExistance
- end fileExists
- -- ------------------------------------------------------
- (*
- has problems, see above.
- Philip Regan
- https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
- on fileExists(theFile) -- (String) as Boolean
- tell application "System Events"
- if exists file theFile then
- return true
- else
- return false
- end if
- end tell
- end fileExists
- *)
- -- ------------------------------------------------------
- (*
- Yvan Koenig
- https://macscripter.net/viewtopic.php?id=43133
- *)
- on findExtension(fileName)
- set saveTID to AppleScript's text item delimiters
- set AppleScript's text item delimiters to {"."}
- set theExt to last text item of fileName
- set AppleScript's text item delimiters to saveTID
- --log "theExt is " & theExt
- if theExt ends with ":" then set theExt to text 1 thru -2 of theExt
- --log "theExt is " & theExt
- return theExt
- end findExtension
- -- ------------------------------------------------------
- (*
- find free name in source and target folder
- *)
- on findFreeName(sourcePath, targetPath, fileName)
- log "findFreeName: sourcePath is " & (sourcePath as text) & " sourcePath is " & (targetPath as text) & " fileName is " & fileName
- log "sourcePath of class = " & class of sourcePath & "sourcePath of class = " & class of sourcePath & "fileName of class = " & class of fileName
- set longWait to 20
- -- set new_name to (prefix & "--" & characters 1 through (-2 - extension_length) of currentName) & "--" & companyCode & "." & name extension of file thisItem as text
- set clearCount to 0
- set theExtension to my findExtension(fileName)
- tell application "Finder"
- set justName to characters 1 through (-2 - (length of theExtension)) of fileName
- end tell
- log "findFreeName: theExtension is " & theExtension & "; justName is " & justName
- set sourceFile to (sourcePath as text) & fileName
- set targetFile to (targetPath as text) & fileName
- log "sourceFile is " & sourceFile & "; targetFile is " & targetFile
- try
- repeat while my fileExists(sourceFile) or fileExists(targetFile)
- log "findFreeName: looping" & " clearCount is " & clearCount
- if clearCount > 3 then
- set commonError to "findFreeName: could not find free name when searching for "
- set commonError to commonError & fileName & ". clearCount is " & clearCount & " clean up folders. "
- set commonError to commonError & return & (sourceFile as text) & return & (targetFile as text)
- my debugLog(commonError)
- -- have to throw an error to get out of this repeat. Could have set a switch I guess.
- display dialog commonError giving up after longWait
- error "cannot find free file name" number 8110
- end if
- set clearCount to clearCount + 1
- log "findFreeName: clearCount is " & clearCount
- set fileName to justName & " #"
- log "findFreeName: fileName is " & fileName
- set fileName to fileName & (clearCount as text)
- log "findFreeName: fileName is " & fileName
- set fileName to fileName & "."
- log "findFreeName: fileName is " & fileName
- set fileName to fileName & (theExtension as text)
- log "findFreeName: fileName is " & fileName
- set sourceFile to (sourcePath as text) & fileName
- set targetFile to (targetPath as text) & fileName
- log "sourceFile is " & sourceFile & "; targetFile is " & targetFile
- -- my debugLog("findFreeName: searching for free name. " & fileName & " clearCount is " & clearCount & return & " destPath is " & destPath)
- end repeat -- while
- on error errMsg number n
- log "error of some type"
- --display dialog errMsg & " with " & n
- if n is not 8110 then
- set displayFileName to fileName as text
- set commonError to "findFreeName: attempting to create free filename " & displayFileName & return & " on error the errMsg is " & errMsg & " number is " & n
- display dialog commonError giving up after longWait
- my debugLog(commonError)
- return "" -- hopefully this will never happen. Haven't implement extensiver error recovery.
- else
- error "cannot find free file name. returning." number 8110
- end if
- end try
- log "findFreeName: return from findFreeName is " & fileName
- return fileName
- end findFreeName
- -- ------------------------------------------------------
- (*
- Yvan Koenig
- https://macscripter.net/viewtopic.php?id=43133
- with mods for no extension present
- *)
- on getExt(theName)
- if (offset of "." in theName) is greater than 0 then
- set saveTID to AppleScript's text item delimiters
- set AppleScript's text item delimiters to {"."}
- set theExt to last text item of theName
- set AppleScript's text item delimiters to saveTID
- if theExt ends with ":" then set theExt to text 1 thru -2 of theExt
- else
- set theExt to ""
- end if
- return theExt
- end getExt
- -- ------------------------------------------------------
- (*
- length of inputLfBuffer & " in hex " & integerToHex(length of inputLfBuffer)
- *)
- on getIntegerAndHex(aNumber)
- global debug
- if debug ≥ 5 then log "in ~~~ getIntegerAndHex ~~~"
- return aNumber & " in Hex " & integerToHex(aNumber)
- end getIntegerAndHex
- -- ------------------------------------------------------
- (*
- Input: a file with or without an extension.
- hhas
- https://forums.macrumors.com/threads/applescript-to-get-file-name.927338/
- may not work with folders with extensions like apps.
- Test cases:
- set fileName to " "
- set fileExt to my getExt(attachmentName)
- log "fileName is " & fileName & " fileExt is " & fileExt
- set fileExt to my getExt("testfileNo")
- log "fileName is " & fileName & " fileExt is " & fileExt
- set fileExt to my getExt("path:to:testfileNo:")
- log "fileName is " & fileName & " fileExt is " & fileExt
- log ">>>>>>>>>>>>>>>>>>>>>>"
- set {fileName, fileExt} to my getNameExt(attachmentName)
- log "fileName is " & fileName & " fileExt is " & fileExt
- set {fileName, fileExt} to my getNameExt("testfileNo")
- log "fileName is " & fileName & " fileExt is " & fileExt
- set {fileName, fileExt} to my getNameExt("path:to:testfileNo:")
- log "fileName is " & fileName & " fileExt is " & fileExt
- *)
- on getNameExt(fileName)
- set saveTID to AppleScript's text item delimiters
- set AppleScript's text item delimiters to "."
- if fileName contains "." then
- set {displayName, nameExt} to {text 1 thru text item -2, text item -1} of fileName
- else
- set {displayName, nameExt} to {fileName, ""}
- end if
- set AppleScript's text item delimiters to saveTID
- return {displayName, nameExt}
- end getNameExt
- (*
- http://krypted.com/mac-os-x/to-hex-and-back/
- 0 2 4 6 8 a c e 0 2 4 6 8 a c e
- 0000000: 3c 703e 5369 6d70 6c65 2070 7574 2c20 <p>Simple put,
- *)
- on hexDumpFormatOne(textMessage, hex)
- global debug
- set aNul to character id 1
- if debug ≥ 7 then log "in ~~~ hexDumpFormatOne ~~~"
- if debug ≥ 8 then log " hexDumpFormatOne: input string is " & return & hex
- -- -r -p
- set displayValue to aNul & hex
- set toUnix to "/bin/echo -n " & (quoted form of displayValue) & " | xxd "
- if debug ≥ 8 then log " hexDumpFormatOne: toUnix is " & toUnix
- try
- set fromUnix to do shell script toUnix
- -- two hex digits
- set displayText to replaceCharacter(fromUnix, 10, " ")
- if debug ≥ 8 then
- log " hexDumpFormatOne: " & return & displayText
- log " hexDumpFormatOne: length of displayText is " & length of displayText
- end if
- -- one character
- set displayText to replaceCharacter(displayText, 51, " ")
- if debug ≥ 8 then
- log " hexDumpFormatOne: " & return & displayText
- log " hexDumpFormatOne: almost there ..... length of displayText is " & length of displayText
- end if
- log "variable " & textMessage & " in hex is " & return & " 0 2 4 6 8 a c e 0 2 4 6 8 a c e" & return & displayText
- on error errMsg number n
- log " hexDumpFormatOne: ==> convert hex string to string failed. " & errMsg & " with number " & n
- end try
- if debug ≥ 8 then
- log "leaving ~.~ hexDumpFormatOne ~.~"
- end if
- end hexDumpFormatOne
- -- ------------------------------------------------------
- (* I think this is the way to force display dialog to the top of the heap of
- windows.
- *)
- on howToDisplayDialog()
- -user3439894
- -- https://apple.stackexchange.com/a/251490/44531
- --
- set theRichTextFileName to POSIX path of (path to desktop as text) & "New RichText Filename.rtf"
- tell application "Finder"
- if exists theRichTextFileName as POSIX file then
- tell current application
- display dialog "The file \"" & theRichTextFileName & "\" already exists!" & "
- " & "Do you want to overwrite the file?" buttons {"No", "Yes"} default button 1 with title "File Already Exists..." with icon caution
- if the button returned of the result is "No" then
- return
- else
- tell application "Finder"
- delete the file (theRichTextFileName as POSIX file)
- end tell
- end if
- end tell
- end if
- end tell
- end howToDisplayDialog
- -- ------------------------------------------------------
- (*
- https://macscripter.net/viewtopic.php?id=43713
- *)
- on integerToHex(nDec)
- global debug
- if debug ≥ 5 then log "in ~~~ integerToHex ~~~"
- try
- set nHex to do shell script "perl -e 'printf(\"%X\", " & nDec & ")'" --> "F0"
- on error errMsg number n
- log "==> convert integer to hex. " & errMsg & " with number " & n
- set nHex to ""
- end try
- return nHex
- end integerToHex
- -- ------------------------------------------------------
- (*
- ls_l is list file with options
- the format is best for debuging.
- example usage:
- set {fileExists, fromUnix} to ls_l(attachmentNamePath, "-l")
- log "attachmentNamePath fileExists is " & fileExists & return & " fromUnix is " & fromUnix
- *)
- on ls_l(attachmentNamePath, options)
- --log "ls_l"
- --log options
- set unixAttachmentNamePath to POSIX path of attachmentNamePath
- --log "unixDesktopPath = " & unixAttachmentNamePath
- set quotedUnixAttachmentNamePath to quoted form of unixAttachmentNamePath
- --log "quoted form is " & quotedUnixAttachmentNamePath
- try
- set fromUnix to do shell script "ls " & options & " " & quotedUnixAttachmentNamePath
- set fromUnix to "ls " & options & return & fromUnix
- set fileExists to true
- on error errMsg number n
- set fromUnix to "ls " & options & " error..." & errMsg & " with number " & n
- set fileExists to false
- end try
- return {fileExists, fromUnix}
- end ls_l
- -- ------------------------------------------------------
- (*
- print out the items in a list
- *)
- on printList(name, splits)
- set theCount to 1
- repeat with theEntry in splits
- log "------- " & name & theCount & " is " & return & theEntry
- set theCount to theCount + 1
- end repeat
- end printList
- -- ------------------------------------------------------
- (*
- got errors when compiling.
- found here: /Users/mac/Documents/BJ\ Prior\ Years/BJ2004/sendmailapp2\ copy
- FYI: I have a full applescript app for sending email.
- *)
- on sendEmail(theEmail)
- set theSubject to "Buffalo Jambalaya Information"
- set theContent to "This is the test message."
- set theSender to "BuffaloJambalaya@earthlink.net"
- set theRecipientName to theEmail
- set theRecipientAddress to theEmail
- tell application "Mail"
- set newMessage to make new outgoing with properties ¬
- {|subject|:theSubject, |content|:theContent, |sender|:theSender, visible:true}
- (*tell newMessage
- make new recipient at end of to recipients with properties ¬
- {name:theRecipientName, address:theRecipientAddress}
- end tell*)
- end tell
- end sendEmail
- -- ------------------------------------------------------
- (*
- textToList seems to be what you are trying to do
- thisText is the input string
- delim is what to split on
- returns a list of strings.
- - textToList was found here:
- - http://macscripter.net/viewtopic.php?id=15423
- *)
- on textToList(thisText, delim)
- set resultList to {}
- set {tid, my text item delimiters} to {my text item delimiters, delim}
- try
- set resultList to every text item of thisText
- set my text item delimiters to tid
- on error
- set my text item delimiters to tid
- end try
- return resultList
- end textToList
- -- ------------------------------------------------------
- (*
- Thanks to apple.com
- trim_indicator values are:
- 0 = beginning, 1 = end, 2 = both
- example:
- trim_line(theName, " ", 2)
- *)
- on trim_line(this_text, trim_chars, trim_indicator)
- -- 0 = beginning, 1 = end, 2 = both
- set x to the length of the trim_chars
- -- TRIM BEGINNING
- if the trim_indicator is in {0, 2} then
- repeat while this_text begins with the trim_chars
- try
- set this_text to characters (x + 1) thru -1 of this_text as string
- on error
- -- the text contains nothing but the trim characters
- return ""
- end try
- end repeat
- end if
- -- TRIM ENDING
- if the trim_indicator is in {1, 2} then
- repeat while this_text ends with the trim_chars
- try
- set this_text to characters 1 thru -(x + 1) of this_text as string
- on error
- -- the text contains nothing but the trim characters
- return ""
- end try
- end repeat
- end if
- return this_text
- end trim_line
- -- ------------------------------------------------------
- (*
- Foound here:
- https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
- Seems the same as Apple's version but with more comments.
- Text Manipulation
- Manipulating text strings is one of the most common tasks performed in scripts. The following sub-routines perform some of the most common text manipulation tasks.
- Trim Line
- The following sub-routine can be used to trim text from the beginning or end of a string. It has three passed parameters:
- The text to trim
- The characters to trim from the passed text
- The direction indicator
- The direction indicator has three possible numeric values:
- 0, which tells the routine to trim the indicated characters from the beginning of the passed string:
- set this_text to "----1----"
- trim_line(this_text, "-", 0)
- --> returns: "1----"
- 1, which tells the routine to trim the indicated characters from the end of the passed string:
- set this_text to "12345.txt"
- trim_line(this_text, ".txt", 1)
- --> returns: "12345"
- 2, which tells this routine to trim the indicated characters from both ends of the passed string:
- set this_text to "*-*-fred*-*-"
- trim_line(this_text, "*-", 2)
- --> returns: "fred"
- *)
- on trim_line_version_2(this_text, trim_chars, trim_indicator)
- -- 0 = beginning, 1 = end, 2 = both
- set x to the length of the trim_chars
- -- TRIM BEGINNING
- if the trim_indicator is in {0, 2} then
- repeat while this_text begins with the trim_chars
- try
- set this_text to characters (x + 1) thru -1 of this_text as string
- on error
- -- the text contains nothing but the trim characters
- return ""
- end try
- end repeat
- end if
- -- TRIM ENDING
- if the trim_indicator is in {1, 2} then
- repeat while this_text ends with the trim_chars
- try
- set this_text to characters 1 thru -(x + 1) of this_text as string
- on error
- -- the text contains nothing but the trim characters
- return ""
- end try
- end repeat
- end if
- return this_text
- end trim_line_version_2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement