hepcat72

Modify selected text length automator applescript v2

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