Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Numerical Incrementing for TextExpander
- -- created by Smile Software http://www.smilesoftware.com for use with TextExpander
- -- Increments a value associated with whatever string is currently on the clipboard
- --
- -- If the clipboard contains something like "something 04.jpg", then on the
- -- first expansion, "something 05.jpg" will be the result (note leading zero matching original).
- -- Subsequently, if "something 04.jpg" is still on the clipboard, then
- -- "something 06.jpg" will be the next result.
- --
- -- Performs this magic by storing a 'TE_incrementer.plist' file in your Preferences folder.
- --
- -- find or create the .plist file
- set thePListFolder to path to preferences folder as string -- (change if desired)
- set thePListFileName to "TE_incrementer.plist" -- (change if desired)
- -- see if file exists
- set thePListPath to thePListFolder & thePListFileName
- set fileExists to "no"
- tell application "Finder" to if exists thePListPath then set fileExists to "yes"
- if ((fileExists) = "no") then -- create the file
- set theEmptyPListData to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
- <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
- <plist version=\"1.0\">
- <dict/>
- </plist>"
- set thePListFile to open for access thePListPath with write permission
- set eof of thePListFile to 0
- write theEmptyPListData to thePListFile starting at eof
- close access thePListFile
- end if
- -- get the clipboard text
- set clipStr to (the clipboard as text)
- if (clipStr = "") then set clipStr to "TEincrementer 0"
- -- parse the string to find the number part
- set endPart to ""
- set numberPart to ""
- set firstPart to ""
- set digits to "0123456789"
- set i to (count clipStr)
- repeat until (i < 1)
- set thisLetter to character i of clipStr
- if (firstPart = "" and (offset of thisLetter in digits) ≠ 0) then
- set numberPart to thisLetter & numberPart
- else if (firstPart = "" and numberPart = "") then
- set endPart to thisLetter & endPart
- else
- set firstPart to thisLetter & firstPart
- end if
- set i to i - 1
- end repeat
- set baseNumber to 0
- set minNumWidth to 0
- if (numberPart ≠ "") then
- set baseNumber to (numberPart as number)
- if ((character 1 of numberPart) = "0") then
- set minNumWidth to length of numberPart
- end if
- else
- set firstPart to endPart
- set endPart to ""
- end if
- -- see if we have a matching property in the plist
- set curItem to null
- tell application "System Events"
- tell property list file thePListPath
- try
- tell contents
- set curItem to value of property list item clipStr
- end tell
- on error the error_message number the error_number
- -- ignore the error, assume it means no value with that key
- end try
- end tell
- end tell
- on toTextWithLeadingZeros(counter, minWidth)
- set fmtNum to (counter as text)
- if (minWidth > 0 and (length of fmtNum) < minWidth) then
- set zeroCount to minWidth - (length of fmtNum)
- repeat zeroCount times
- set fmtNum to "0" & fmtNum
- end repeat
- end if
- return fmtNum
- end toTextWithLeadingZeros
- if (curItem = null) then
- set curItem to my toTextWithLeadingZeros(baseNumber + 1, minNumWidth)
- -- add a new entry to the plist file
- tell application "System Events"
- tell property list file thePListPath
- make new property list item at end with properties ¬
- {kind:number, name:clipStr, value:(curItem as number)}
- end tell
- end tell
- else
- -- increment the existing value and save it back to the plist
- set curItem to my toTextWithLeadingZeros((curItem as number) + 1, minNumWidth)
- tell application "System Events"
- tell property list file thePListPath
- tell contents
- set value of property list item clipStr to (curItem as number)
- end tell
- end tell
- end tell
- end if
- return firstPart & curItem & endPart
Advertisement
Add Comment
Please, Sign In to add comment