Help Completing / Coding Droplet for Tweeting via Twitter Scripter

Hi Yvan,

It works on the first set of tweets, and cycles around once the list is complete, starting over as expected. However, it only posts the same tweet (tweet 21) of the second list of tweets! Both sets post with images, whether in the Pictures folder or a subfolder of the pictures folder, which it wasn’t doing earlier in the day when I tested, but is since coming back to post this message.

I know you are at the end of you tether with this but, I would be grateful if you could look at why the second list of tweets are not incrementing beyond the first message (tweet 21) in the list, as I can’t spot anything myself.

Also, would there be an easy way at the start of the code to enable / disable the accounts? For example use account 1 and 2 or use only account one or account two depending on what is set?

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 9 juillet 2017 10:24:58

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 9 juillet 2017 11:42:46

Hello Yvan,

I have tried the code using option 1 and 2 when presented, and get the error message below. I have not been able to confirm if the second set of messages increment, as nothing posts currently.

Please forgive me, I was not clear in my request for the ability to enable / disable account use. I was after a hard coded option for enabling, or disabling an account, and not anything requiring human interaction or button selections. :slight_smile:

Perhaps something at the start of the code, like the (rough) example below, which you provided in a different form for another script?


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.

Thank you.

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 9 juillet 2017 21:29:31

Hello Yvan,

On reflection I think I am trying to make the code far more complex than needed, as I believe I could simply remark out the second account tweet line, if it is not needed, or used.

As the code was working, minus the issue of not posting the second line of tweets beyond the first entry, could we revert to a simpler version that I can test each tweet account with to ensure the lists post, with an image?

Thank you.

Did you tested the late version posted in message #17?
It’s supposed to do the job flawlessly.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) lundi 10 juillet 2017 10:48:56

Hello Yvan,

I did. But I may not have used the correct version, as it failed!

Could you link me to the version you want me to test and I will try it this evening, and post the results?

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) lundi 10 juillet 2017 12:16:42

Hello Yvan,

I have tried several attempts to make the code run, and the following message box appears when running the latest code:

I know you don’t have access to twitter or use twitter, so I ran some local tests, which I hope help. Please forgive me if I am teaching you what you already know. :slight_smile:

The following code tells me I have three accounts setup:


tell application "Twitter Scripter"
	-- returns a list of usernames for the OS defined Twitter accounts
	set availableAccounts to available accounts
end tell

-- Result Screen: {"Hal","Batman", "Asterix"}

The above response for the returned list shows there are three accounts, all are active. Account names have been renamed for privacy.

Running the following command and replacing the ‘item’ number in each run, shows me which account a tweet will be posted from, based on the ‘Item’ number ‘set’ :



tell application "Twitter Scripter"
	

	-----------------------------------------
	-- Account commands
	-----------------------------------------
	
	-- returns a list of usernames for the OS defined Twitter accounts
	
	set availableAccounts to available accounts
	
	-- Test to check we've got at least one account
	
	if ((count of availableAccounts) is 0) then
		
		tell application "Finder"
			activate
			display dialog "Please set up at least one 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 Available Twitter Accounts"
		end tell
		return
	end if

#set twitterAccount to item 1 of availableAccounts -- uses account named Hal (default for live Tweets)
#set twitterAccount2 to item 2 of availableAccounts -- uses account named Batman (secondary account for live tweets)
set twitterAccount2 to item 3 of availableAccounts -- uses account named Asterix (default for testing and not used for live tweets)

#Replacing the item number with either 1, 2 or 3, as shown above, confirms in the result screen which account the number relates to. Below is the output per number  

# 1 = Hal returned from code when 'item' was set to 1
# 2 = Batman returned from code when 'item' was set to 2
# 3 = Asterix (item 3 is our test account) returned from code when 'item' was set to 3

end tell
return twitterAccount

-- Result Screen: "Asterix"

# depending on the item number above the output screen shows the account selected. In this instance 3 is set, which is our test account


Now using this command we set the account to use for tweeting:



set twitterAccount to item 3 of availableAccounts -- NOTE: set 'item' 3 which is the account [Asterix] for testing.
#Account three will be used for twitterAccount during tests, and also for testing twitterAccount2 when the primary twitterAccount is disabled.  


This then wraps into the following commands for a simple text based tweet test, which works well with the ‘set’ twitterAccount. Typically we are testing one account setup at a time to avoid posting to live accounts!


tweet "I'm tweeting from my desktop!" using account twitterAccount
#OR
tweet 'This is the second account for tweets!" using account twitterAccount2 

-- remember to change the 'item' number for each account. In our case we always use 3 for tests!

# I have removed image tweeting from the code for now to make life easier.

What I can’t see in your new code is where to ‘set’ the twitter account to use via twitter scripter. Please forgive me if it’s in the code, I just cant spot where to set this, and or what code to use.

Thank you.

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) lundi 10 juillet 2017 18:22:36

Hello Yvan,

I have inserted comments commencing with – to your questions below. I am sorry for any confusion I am causing, and I am grateful for your amazing patience with me. :slight_smile:

The requirement below, in my mind is the same as above. However, it seems that the ‘item’ is what defines the account to use, so being able to quickly set this is essential.

I have made a small adjustment to the list below, which might clarify matters.

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) lundi 10 juillet 2017 19:09:16

The real life configuration would be 2 for Index1 and 3 for Index2

However, the ability to change the item number associated with the index would be essential. Could this be achieved by simply editing the code, and changing the item number(s) as required?

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) lundi 10 juillet 2017 20:51:47

Hi Yvan,

When I change the settings in the code below to any value the result is constantly the same! Is that meant to happen? I was expecting the accounts shown in the result window to change.

The code below when run gave the output under result. The result changed if I changed the values up to the value 9, upon which it presented a coded message, as intended, requesting an account be set.

– Result: {“account2”, “account3”}

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 11 juillet 2017 11:42:03

Hello Yvan,

The corrected code in message #29 appears to work! Based on the data entered, I get the following values back.

Selecting howMany = 3



set howMany to 3 # Play with values 1, 2 or 3. In the full script, the case 0 is filtered before calling the handler.

set {tweeterAccount, tweeterAccount2} to my setTweeterAccount(howMany) # Corrected a typo

on setTweeterAccount(howMany)
	# Here, howMany may be 1, 2 or 3
	# 1 means use account index indexForAccount and account index indexForAccount2
	# 2 means use account index indexForAccount
	# 3 means use account index indexForAccount2
	# play with the 2 values below
	set indexForAccount to 1
	set indexForAccount2 to 6

-- Result displayed as: {"account6", false}

Selecting howMany = 2


set howMany to 2 # Play with values 1, 2 or 3. In the full script, the case 0 is filtered before calling the handler.

set {tweeterAccount, tweeterAccount2} to my setTweeterAccount(howMany) # Corrected a typo

on setTweeterAccount(howMany)
	# Here, howMany may be 1, 2 or 3
	# 1 means use account index indexForAccount and account index indexForAccount2
	# 2 means use account index indexForAccount
	# 3 means use account index indexForAccount2
	# play with the 2 values below
	set indexForAccount to 1
	set indexForAccount2 to 6

-- Result displayed as: {"account1", false}

Selecting howMany = 1


set howMany to 1 # Play with values 1, 2 or 3. In the full script, the case 0 is filtered before calling the handler.

set {tweeterAccount, tweeterAccount2} to my setTweeterAccount(howMany) # Corrected a typo

on setTweeterAccount(howMany)
	# Here, howMany may be 1, 2 or 3
	# 1 means use account index indexForAccount and account index indexForAccount2
	# 2 means use account index indexForAccount
	# 3 means use account index indexForAccount2
	# play with the 2 values below
	set indexForAccount to 1
	set indexForAccount2 to 6

-- Result displayed as: {"account1", "account6"}

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

Hello Yvan,

The alternate way in your response, is what we are after. So when howMany is set to 1 it posts tweets from both theTweets and theTweets2 to two separate twitter accounts!

When set to howMany 2 the script should only issue tweets from the account set and theTweets. No other tweets or accounts are used.

When set to howMany 3 the script should only issue tweets from the other account set, and theTweets2. No other tweets or accounts used.

Let me test what you have kindly provided, and drop an update when tested.

Thanks you