Advertisement
applehelpwriter

script to rant on twitter

Jul 27th, 2016
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###########################################################
  2. # ABOUT
  3. ###########################################################
  4. (*
  5.  
  6.  Phil Stokes
  7.  applehelpwriter.com
  8.  sqwarq.com 2016
  9.  
  10. *)
  11. ###########################################################
  12. # DESCRIPTION
  13. ###########################################################
  14. (*
  15.  
  16. The script can be used to break up a long text and send it in a series of tweets n seconds apart (default = 10 secs)
  17. Dependencies: This script requires the Twitter.app
  18.  
  19.  
  20. *)
  21. ###########################################################
  22. # USAGE
  23. ###########################################################
  24. (*
  25.  
  26. In the Variables section below, set the variable tweetRant to the rant you want to send.
  27. Run the script.
  28.  
  29.  
  30. *)
  31. ###########################################################
  32. # IMPORT STATEMENTS
  33. ###########################################################
  34.  
  35. use AppleScript version "2.4" -- Yosemite (10.10) or later
  36. use scripting additions
  37.  
  38.  
  39. ###########################################################
  40. # VARIABLES
  41. ###########################################################
  42.  
  43. set tweetRant to "Any string of any length, forget about the 142-character limit that's supposed to stop people ranting on twitter, you can rant as long as you like and for ever if you want though I wouldn't advise it unless you want to lose all your followers or get spammed back in return!! #addTags @andAtYou's here too"
  44.  
  45. set theEllipsis to "..."
  46. set tweetStore to {}
  47.  
  48.  
  49. ###########################################################
  50. # HANDLERS
  51. ###########################################################
  52.  
  53. on sendTweet(aTweet)
  54.     tell application "System Events"
  55.         tell application process "Twitter"
  56.             tell its window 1
  57.                 tell its button 1 to click --open the new tweet window
  58.                 delay 2
  59.             end tell
  60.             tell its front window
  61.                 tell text area 1 of scroll area 1
  62.                     set its value to aTweet
  63.                 end tell
  64.                 delay 1
  65.                 tell its button 1 to click -- should send the tweet
  66.             end tell
  67.         end tell
  68.     end tell
  69. end sendTweet
  70.  
  71. on getOffsetOfLastOccurenceOf:target inString:source
  72.     set astid to AppleScript's text item delimiters
  73.     set AppleScript's text item delimiters to target
  74.     try
  75.         set ro to (count source) - (count text item -1 of source)
  76.     on error errMsg number errNum
  77.         display dialog errMsg
  78.     end try
  79.     set AppleScript's text item delimiters to astid
  80.     return ro - (length of target) + 1
  81. end getOffsetOfLastOccurenceOf:inString:
  82.  
  83. # return offset of the last space in the string,  counting forward from text item 1
  84. on findLastSpaceUpto:aLimit inString:aString
  85.     set isSpace to text aLimit of aString
  86.     if isSpace is " " then
  87.         return aLimit
  88.     else
  89.         set searchBkw to text 1 thru aLimit of aString
  90.         set bksSpaceNum to its getOffsetOfLastOccurenceOf:" " inString:searchBkw
  91.         return bksSpaceNum
  92.     end if
  93. end findLastSpaceUpto:inString:
  94.  
  95.  
  96.  
  97.  
  98. ###########################################################
  99. # COMMANDS
  100. ###########################################################
  101. tell application "Twitter"
  102.     activate
  103. end tell
  104.  
  105. delay 1
  106.  
  107. repeat while tweetRant's length is greater than 139
  108.     if length of tweetRant is greater than 135 then -- we need space for the ellipses at both ends
  109.         set theLength to 135
  110.     else
  111.         set theLength to tweetRant's length
  112.     end if
  113.     set lastSpace to its findLastSpaceUpto:theLength inString:tweetRant
  114.     set end of tweetStore to text 1 thru lastSpace of tweetRant
  115.     set tweetRant to text lastSpace thru -1 of tweetRant
  116. end repeat
  117.  
  118. -- get the remaining text as the last  tweet
  119. if length of tweetRant is less than 140 then
  120.     set end of tweetStore to tweetRant
  121. end if
  122.  
  123.  
  124. set testFire to {} -- we use this for testing in line 139 below
  125. repeat with i from 1 to count of tweetStore
  126.     set this_tweet to item i of tweetStore
  127.     if i is 1 then
  128.         set this_tweet to this_tweet & theEllipsis
  129.     else if i is less than (count of tweetStore) then
  130.         set this_tweet to theEllipsis & this_tweet & theEllipsis
  131.     else
  132.         set this_tweet to theEllipsis & this_tweet
  133.     end if
  134.     try
  135.         if this_tweet's length is less than 143 then
  136.            
  137.             # WARNING:
  138.             -------------------------------------
  139.             # set end of testFire to this_tweet -- uncomment this line to test what you'll send
  140.             sendTweet(this_tweet) -- you're firing live rounds!! -- Comment out when testing!!!
  141.             delay 10 -- set the delay between tweets
  142.             -------------------------------------          
  143.            
  144.         else
  145.             error -128 -- if anything goes wrong, stop sending the tweets
  146.         end if
  147.     on error
  148.         error -128 -- if anything goes wrong, stop sending the tweets
  149.     end try
  150. end repeat
  151.  
  152. ###########################################################
  153. #EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement