Here is the true script :
(*
http://macscripter.net/edit.php?id=190729
*)
property theTweets : {"Testing Tweets from my desktop again", "#HappyDays more testing of desktop tweets", "Follow the tweeting challenge"}
property theTweets2 : {"tweet21", "tweet 22", "tweet 23", "tweet 24"}
property howMany : 1
# 0 = disable all tweeting - Nothing will be posted. Use for safety when code is not intended to run
# 1 = use tweeterAccount, tweeterAccount2 - Default setting. This will use both defined accounts
# 2 = use tweeterAccount - Use only first account defined in the list of available accounts
# 3 = use tweeterAccount2 - Use only the Second account defined in the list of available accounts
-- If more than three accounts are defined in the system this code will ensure only those required, and defined, are accessed and used via the script.
#=====
# Entry point used by double click or by calling from the editor
on run
set droppedFiles to choose file "Select some picture files" of type {"png", "jpeg", "jpg", "tiff", "tif"} default location (path to pictures folder) with multiple selections allowed
my main(droppedFiles) # droppedFiles is a list of aliases
end run
#=====
# Entry point used by drag & drop
on open droppedFiles # droppedFiles is a list of aliases
my main(droppedFiles)
end open
#=====
on main(droppedFiles) # droppedFiles is a list of aliases
# Defining the local variables is useful for Script Debugger
local maxLen, theDelay, path2log, indexPath, currentIndexes, currentIndex1, currentIndex2
local twoAccounts, tweeterAccount, tweeterAccount2, cnt1, cnt2, num1, num2, i, aFile, aPictPath, maxi
if howMany = 0 then error number -128
set maxLen to 140 - 1 # free space for a space character
set theDelay to 0.1 # I assume that a delay may be needed between two tweets. Maybe this value is too short.
set indexPath to (path to library folder from user domain as text) & "Application Support:tweetIndex_xednIteewt.txt"
# In both cases read two indexes so, if we really use only one we will be able to restore the second one before exit.
try
set currentIndexes to paragraphs of (read file indexPath)
set currentIndex1 to (item 1 of currentIndexes) as integer
try
set currentIndex2 to (item 2 of currentIndexes) as integer # EDITED on 2017/07/09
on error
set currentIndex2 to 1
end try
on error
set {currentIndex1, currentIndex2} to {1, 1}
end try
set twoAccounts to my setTweeterAccount(howMany)
my logThat("twoAccounts = " & my recolle(twoAccounts, ", ") & linefeed & linefeed, false)
set {tweeterAccount, tweeterAccount2} to twoAccounts
my logThat("tweeterAccount : " & tweeterAccount & linefeed & "tweeterAccount2 : " & tweeterAccount2 & linefeed & linefeed, true)
tell application "Twitter Scripter"
# Loop issuing the tweets
set {cnt1, cnt2} to {count theTweets, count theTweets2}
set {num1, num2} to {0, 0}
set i to 0
my logThat("howmany : " & howMany & linefeed & linefeed, true)
repeat with aFile in droppedFiles
set aPicPath to aFile as text
my logThat("i = " & i & ", treat file : " & aPicPath & linefeed, true)
if howMany is in {1, 2} then
delay theDelay
if i mod cnt1 = 0 then set num1 to num1 + 1
set max1 to maxLen - (count (num1 as text))
set theTweet to (item currentIndex1 of theTweets) as text
if (count theTweet) > max1 then set theTweet to items 1 thru max1 of theTweet as text
set theTweet to theTweet & space & num1
my logThat("theTweet : " & theTweet & linefeed & "tweeterAccount : " & tweeterAccount & linefeed, true)
tweet theTweet using account tweeterAccount with image aPicPath
set currentIndex1 to currentIndex1 + 1
if currentIndex1 > (count theTweets) then set currentIndex1 to 1
end if # {1, 2}
if howMany is in {1, 3} then
delay theDelay
if i mod cnt2 = 0 then set num2 to num2 + 1
set max2 to maxLen - (count (num2 as text))
set theTweet to (item currentIndex2 of theTweets2) as text
if (count theTweet) > max2 then set theTweet to items 1 thru max2 of theTweet as text
set theTweet to theTweet & space & num2
my logThat("theTweet : " & theTweet & linefeed & "tweeterAccount2 : " & tweeterAccount2 & linefeed & linefeed, true)
tweet theTweet using account tweeterAccount2 with image aPicPath
set currentIndex2 to currentIndex2 + 1
if currentIndex2 > (count theTweets2) then set currentIndex2 to 1
end if # {1, 3}
set i to i + 1 # next file
end repeat
end tell # "Twitter Scripter"
# Prepare for the next run
my writeto(indexPath, (currentIndex1 as text) & linefeed & currentIndex2, text, false)
end main
#=====
on setTweeterAccount(howMany)
local availableAccounts, countAccounts, indexForAccount, indexForAccount2 # ADDED
# Here, howMany may be 1, 2 or 3
tell application "Twitter Scripter"
-- returns a list of usernames for the OS defined Twitter accounts
set availableAccounts to available accounts
end tell
set countAccounts to count availableAccounts
set indexForAccount to 2 # ADDED, edit to fit your needs
set indexForAccount2 to 3 # ADDED, edit to fit your needs
if indexForAccount > indexForAccount2 then
if howMany is in {1, 2} then
if (countAccounts < indexForAccount) then
tell application (path to frontmost application as string)
display dialog "Please set up at least " & indexForAccount & " Twitter account in your System Preferences, and allow it to be accessed by \"Twitter Scripter\"" buttons {"OK"} default button "OK" with icon 2 with title "No " & indexForAccount & " Twitter Accounts available !"
end tell
error number -128
end if
if howMany = 1 then
return {item indexForAccount of availableAccounts, item indexForAccount2 of availableAccounts}
else
return {item indexForAccount of availableAccounts, false}
end if
else # here howMany = 3 then
if countAccounts < indexForAccount2 then --> want item smallerIndex of availableAccounts
tell application (path to frontmost application as string)
display dialog "Please set it up at least " & indexForAccount2 & " Twitter accounts in your System Preferences, and allow them to be accessed by \"Twitter Scripter\"" buttons {"OK"} default button "OK" with icon 2 with title "No " & indexForAccount2 & " Twitter Accounts available !"
end tell
error number -128
end if
return {item indexForAccount2 of availableAccounts, false}
end if
else
# Here indexForAccount ≤ indexForAccount2. In fact it's smaller than !
if howMany is in {1, 3} then
if (countAccounts < indexForAccount2) then
tell application (path to frontmost application as string)
display dialog "Please set up at least " & indexForAccount2 & " Twitter account in your System Preferences, and allow it to be accessed by \"Twitter Scripter\"" buttons {"OK"} default button "OK" with icon 2 with title "No " & indexForAccount2 & " Twitter Accounts available !"
end tell
error number -128
end if
if howMany = 1 then
return {item indexForAccount of availableAccounts, item indexForAccount2 of availableAccounts}
else
return {item indexForAccount2 of availableAccounts, false}
end if
else # here howMany = 2 then
if countAccounts < indexForAccount then
tell application (path to frontmost application as string)
display dialog "Please set it up at least " & indexForAccount & " Twitter accounts in your System Preferences, and allow them to be accessed by \"Twitter Scripter\"" buttons {"OK"} default button "OK" with icon 2 with title "No " & indexForAccount & " Twitter Accounts available !"
end tell
error number -128
end if
return {item indexForAccount of availableAccounts, false}
end if
end if
end setTweeterAccount
#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as class furl
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeto
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
on logThat(msg, trueOrFalse)
set path2log to (path to desktop as text) & "log report_troper gol.txt"
my writeto(path2log, msg, text, trueOrFalse)
end logThat
#=====
Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 11 juillet 2017 13:55:31