hepcat72

Modify selected text length automator applescript

Feb 27th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Installation: Create an Automator service, paste this code into a "Run AppleScript" action, and save.
  2. --How to use it: 1. Copy a number indicating the length you want the selection to be. 2. Select the region of text where you want the copied length to be selected. 3. Right-click the selection and select this service
  3.  
  4. --NOTES:
  5. --If the contents of the clipboard is not a number, you'll be prompted to enter a length, although this feature has not been tested.
  6. --If input is not supplied, this script will backup the current clipboard, copy the current selection to get the currently selected text, then restore the clipboard.
  7.  
  8. on run {input, parameters}
  9.    
  10.     try
  11.         set debug to false
  12.         if input's class is not list then
  13.             set input to getHighlight(debug)
  14.         end if
  15.        
  16.         tell application "System Events"
  17.             --See if the clipboard has a number in it to use for the selection length
  18.             set checkClipboard to the clipboard as text
  19.             if my isNumString(checkClipboard) then
  20.                 set n to checkClipboard as integer
  21.             else
  22.                 set n to my getintvalue()
  23.             end if
  24.            
  25.             --Check the length of the selected text passed in
  26.             set character_count to count characters of ((input as string) as string)
  27.            
  28.             set lengthen to false
  29.             set mod_length to 0
  30.             if character_count is equal to 0 or n is greater than character_count then
  31.                 set lengthen to true
  32.                 set mod_length to n - character_count
  33.             else
  34.                 set mod_length to character_count - n
  35.             end if
  36.            
  37.             --See if we're in Terminal
  38.             set isTerminal to ((name of first process where it is frontmost) as string) is equal to "Terminal"
  39.            
  40.             if isTerminal is true then
  41.                 if lengthen is true then
  42.                     set substr to (input as string)
  43.                     repeat mod_length times
  44.                         set substr to substr & "N"
  45.                     end repeat
  46.                     set substr to " <- " & n
  47.                 else
  48.                     set substr to text 1 thru n of (input as string)
  49.                     set substr to substr & " <- " & n & " "
  50.                     set substr to substr & (text (n + 1) thru character_count of (input as string))
  51.                 end if
  52.                 display dialog "Position " & n & " is marked below:" & return & return & substr buttons {"OK"}
  53.             else
  54.                 --Figure out whether the selection must be lengthened or shrunk
  55.                 if not (character_count is equal to 0 or n is greater than character_count) then
  56.                     key code 124 using {shift down}
  57.                     key code 123 using {shift down}
  58.                 end if
  59.                
  60.                 repeat mod_length times
  61.                     if lengthen is true then
  62.                         key code 124 using {shift down}
  63.                     else
  64.                         key code 123 using {shift down}
  65.                     end if
  66.                 end repeat
  67.             end if
  68.         end tell
  69.     on error errstr
  70.         display dialog errstr
  71.     end try
  72.    
  73. end run
  74.  
  75.  
  76.  
  77. property mytitle : "Length of text to select"
  78.  
  79. -- I am asking the user to provide an integer
  80. -- In case the user cancels the dialog, I return «missing value»
  81. on getintvalue()
  82.     set dlgmsg to "How many characters would you like to select?:"
  83.     try
  84.         display dialog dlgmsg default answer "1" buttons {"Cancel", "Enter"} default button 2 with title mytitle
  85.     on error
  86.         -- User canceled
  87.         return missing value
  88.     end try
  89.     set dlgresult to result
  90.     set usrinput to text returned of dlgresult
  91.     -- the user did not enter anything...
  92.     if usrinput is "" then
  93.         my getintvalue()
  94.     else
  95.         -- let's check if the user entered numbers only
  96.         set nums to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
  97.         set invalidchars to ""
  98.         repeat with char in usrinput
  99.             if char is not in nums then
  100.                 set invalidchars to invalidchars & char & space
  101.             end if
  102.         end repeat
  103.         -- we found invalid characters
  104.         if invalidchars is not "" then
  105.             set errmsg to "We found the following characters in the given input. Please enter numbers only." & return & return & invalidchars & return
  106.             my dsperrmsg(errmsg, "--")
  107.             my getintvalue()
  108.         else
  109.             -- let's try to transform the user input into an integer
  110.             try
  111.                 set intvalue to usrinput as integer
  112.                 return intvalue
  113.             on error
  114.                 set errmsg to "We could not coerce the given input into an integer:" & return & return & usrinput & return
  115.                 my dsperrmsg(errmsg, "--")
  116.                 my getintvalue()
  117.             end try
  118.         end if
  119.     end if
  120. end getintvalue
  121.  
  122. -- I am displaying error messages to the user
  123. on dsperrmsg(errmsg, errnum)
  124.     tell me
  125.         activate
  126.         display dialog errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with title mytitle with icon stop
  127.     end tell
  128. end dsperrmsg
  129.  
  130. -- isNumString :: String -> Bool
  131. on isNumString(s)
  132.     try
  133.         if class of s is string then
  134.             set c to class of (s as number)
  135.             c is real or c is integer
  136.         else
  137.             false
  138.         end if
  139.     on error
  140.         false
  141.     end try
  142. end isNumString
  143.  
  144. on getHighlight(debug)
  145.    
  146.     --Save the current contents of the clipboard
  147.     try
  148.         set theSpare to the clipboard --as text
  149.     on error
  150.         set response to (display dialog "Warning: The contents of the clipboard will be lost." buttons {"Cancel", "OK"})
  151.         if button returned of response is "OK" then
  152.             set theSpare to ""
  153.         end if
  154.     end try
  155.     set the clipboard to ""
  156.     --Declare the variable we're going to return
  157.     set selecTxt to ""
  158.    
  159.     tell application "System Events"
  160.         --Initiate the copy
  161.         keystroke "c" using {command down}
  162.        
  163.         --Wait up to 2 seconds for the copy to finish
  164.         set done to "no"
  165.         set waitnum to 0
  166.         set waitInterval to 0.02
  167.         set maxwaits to 100
  168.        
  169.         --Repeat while the clipboard contents have not changed
  170.         repeat while done = "no"
  171.             --Get the contents of the clipboard
  172.             try
  173.                 set selecTxt to the clipboard as text
  174.             end try
  175.            
  176.             --See if we're done or need to wait
  177.             if waitnum is equal to maxwaits then
  178.                 set done to "yes"
  179.             else if selecTxt is equal to "" then
  180.                 delay waitInterval
  181.                 set waitnum to waitnum + 1
  182.             else
  183.                 set done to "yes"
  184.             end if
  185.            
  186.         end repeat
  187.        
  188.         if debug is true then
  189.             try
  190.                 display dialog "Copied text: " & (the clipboard as text)
  191.             end try
  192.         end if
  193.     end tell
  194.    
  195.     --Restore the original clipboard contents
  196.     set the clipboard to theSpare --as record
  197.    
  198.     if debug is true then
  199.         try
  200.             display dialog "The clipboard contents have been restored to " & (the clipboard as text)
  201.         end try
  202.     end if
  203.    
  204.     --Return the highlighted text
  205.     return selecTxt
  206.    
  207. end getHighlight
Advertisement
Add Comment
Please, Sign In to add comment