hepcat72

Modify selected text length automator applescript v2

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