gcoghill

Numerical incrementing with AppleScript for TextExpander

Nov 10th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Numerical Incrementing for TextExpander
  2. -- created by Smile Software http://www.smilesoftware.com for use with TextExpander
  3. -- Increments a value associated with whatever string is currently on the clipboard
  4. --
  5. -- If the clipboard contains something like "something 04.jpg", then on the
  6. -- first expansion, "something 05.jpg" will be the result (note leading zero matching original).
  7. -- Subsequently, if "something 04.jpg" is still on the clipboard, then
  8. -- "something 06.jpg" will be the next result.
  9. --
  10. -- Performs this magic by storing a 'TE_incrementer.plist' file in your Preferences folder.
  11. --
  12. -- find or create the .plist file
  13. set thePListFolder to path to preferences folder as string -- (change if desired)
  14. set thePListFileName to "TE_incrementer.plist" -- (change if desired)
  15.  
  16. -- see if file exists
  17. set thePListPath to thePListFolder & thePListFileName
  18. set fileExists to "no"
  19. tell application "Finder" to if exists thePListPath then set fileExists to "yes"
  20.  
  21. if ((fileExists) = "no") then -- create the file
  22. set theEmptyPListData to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  23. <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
  24. <plist version=\"1.0\">
  25. <dict/>
  26. </plist>"
  27. set thePListFile to open for access thePListPath with write permission
  28. set eof of thePListFile to 0
  29. write theEmptyPListData to thePListFile starting at eof
  30. close access thePListFile
  31. end if
  32.  
  33. -- get the clipboard text
  34. set clipStr to (the clipboard as text)
  35. if (clipStr = "") then set clipStr to "TEincrementer 0"
  36. -- parse the string to find the number part
  37. set endPart to ""
  38. set numberPart to ""
  39. set firstPart to ""
  40. set digits to "0123456789"
  41. set i to (count clipStr)
  42. repeat until (i < 1)
  43. set thisLetter to character i of clipStr
  44. if (firstPart = "" and (offset of thisLetter in digits)0) then
  45. set numberPart to thisLetter & numberPart
  46. else if (firstPart = "" and numberPart = "") then
  47. set endPart to thisLetter & endPart
  48. else
  49. set firstPart to thisLetter & firstPart
  50. end if
  51. set i to i - 1
  52. end repeat
  53. set baseNumber to 0
  54. set minNumWidth to 0
  55. if (numberPart ≠ "") then
  56. set baseNumber to (numberPart as number)
  57. if ((character 1 of numberPart) = "0") then
  58. set minNumWidth to length of numberPart
  59. end if
  60. else
  61. set firstPart to endPart
  62. set endPart to ""
  63. end if
  64.  
  65. -- see if we have a matching property in the plist
  66. set curItem to null
  67. tell application "System Events"
  68. tell property list file thePListPath
  69. try
  70. tell contents
  71. set curItem to value of property list item clipStr
  72. end tell
  73. on error the error_message number the error_number
  74. -- ignore the error, assume it means no value with that key
  75. end try
  76. end tell
  77. end tell
  78.  
  79. on toTextWithLeadingZeros(counter, minWidth)
  80. set fmtNum to (counter as text)
  81. if (minWidth > 0 and (length of fmtNum) < minWidth) then
  82. set zeroCount to minWidth - (length of fmtNum)
  83. repeat zeroCount times
  84. set fmtNum to "0" & fmtNum
  85. end repeat
  86. end if
  87. return fmtNum
  88. end toTextWithLeadingZeros
  89.  
  90. if (curItem = null) then
  91. set curItem to my toTextWithLeadingZeros(baseNumber + 1, minNumWidth)
  92.  
  93. -- add a new entry to the plist file
  94. tell application "System Events"
  95. tell property list file thePListPath
  96. make new property list item at end with properties ¬
  97. {kind:number, name:clipStr, value:(curItem as number)}
  98. end tell
  99. end tell
  100. else
  101. -- increment the existing value and save it back to the plist
  102. set curItem to my toTextWithLeadingZeros((curItem as number) + 1, minNumWidth)
  103. tell application "System Events"
  104. tell property list file thePListPath
  105. tell contents
  106. set value of property list item clipStr to (curItem as number)
  107. end tell
  108. end tell
  109. end tell
  110. end if
  111.  
  112. return firstPart & curItem & endPart
Advertisement
Add Comment
Please, Sign In to add comment