Help Completing / Coding Droplet for Tweeting via Twitter Scripter

Hi Guys,

I have made some small progress with the following code. I would appreciate some assistance in building on the code to run sequentially tweets from the defined tweets in ‘theTweets’ list, and to include images dropped onto the app from within a subfolder of the picture folder, when tweeting. Image have to be in this location for security purposes, according to Twitter Scripter.

The code provided kindly by Kerflooey worked with hard coded image names, although the tweets failed. I hope I can add images to tweets from the safe folder location by dropping them on the droplet, and therefor not requiring the image name to be hard coded in a similar fashion to the code Yvan created for us to use for emails.

Hopefully, someone can assist with the missing code elements, as we seem to be on the right path.

Please note. I am the worst kind of coder, as I am not a coder at all!

Thank you all in advance.




tell application "Twitter Scripter"
	
	# First account in  available list = Name
	# Second account in available list = Primary Name
	# Note the first account may not be the default account! Naming appears to be alphabetical when results are returned
	
	-----------------------------------------
	-- 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 2 of availableAccounts -- choose the second (primary) available account
	
end tell

#set theText to {"Uno", "Dos", "Tweet 3", "Tweet 4"} # This approach posts ALL tweets at once if there is no associated image to match the tweets!

property theTweets : {"Testing Tweets from my desktop again", "#HappyDays more testing of desktop tweets", "Follow the tweeting challenge"} # This version posts a single tweet, however fails on second run

property currentIndex1 : 1 # Tweets for Account 2, which is our primary account!

#on open droppedFiles # droppedFiles is a list of aliases
#	new_tweet(droppedFiles)
#end open

-- Used to try and run the code when an image is opened with the droplet within a subfolder of the pictures folder


set theTweets to item currentIndex1 of theTweets

set currentIndex1 to currentIndex1 + 1
if currentIndex1 > (count theTweets) then set currentIndex1 to 1

#set thePicNames to {} # Removed for initial text based tweeting only as did not run, per my previous update

#set thePicPath to (path to pictures folder) as text #Subfolder in the Pictures folder will be used
#set thePix to {}
#repeat with aPicName in thePicNames
#set thePix to thePix & (thePicPath & (aPicName as text))
#end repeat

#set picNum to 0

tell application "Twitter Scripter"
	
	#repeat with aTweet in theText
	#set picNum to (picNum + 1)
	#tweet (aTweet as text) using account twitterAccount2 with image (item picNum of thePix)
	#end repeat
	
	#tweet (theTweets as text) using account twitterAccount2 with image (item picNum of thePix) 
	
	tweet (theTweets as text) using account twitterAccount2 
-- Tweet works but does not increment from theTweets list! Posts a random character when the code is run a second time, and then returns a failed result if run again!
	
end tell

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) samedi 8 juillet 2017 11:31:01

Hello Yvan,

The code works with the following observations:

1 - theTweets list was used each time the code ran, with a new tweets for each time the code was used. However, once all of the test tweets were used it did not start from the beginning to repost from tweet one again. The code appeared to run, with no errors, but no new tweets were seen on the Twitter feed.

2 - When using the full version to include an image it did not post an image, but it did post the text tweet, and used a new tweet each time, with the same behaviour as above in point 1, once the test tweet limit was exceeded.

I note if the text in the tweets is adjusted a small amount the tweets post fine until the limit is reached. I wonder if this is a limitation of twitter or twitter scripter, in terms of not re-posting the same text tweet more than once? On three separate tests the tweets did not repeat on the twitter feed once the last tweet in the list had been used, although the code appeared to run fine, with the exception of not posting the image in the full version.

Perhaps adding the name of the image minus the extension, would help if this is the case, as the image names will always increment.

This is most definitely helpful, and a step in the right direction - thanks you.

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) samedi 8 juillet 2017 14:32:34

Here are the results from the test run. I will answer your earlier questions separately.

One thing to note is the images must be loaded from the Pictures folder, which I am doing when running the droplet.


tell current application
	path to library folder from user domain as text
		--> "Macintosh HD:Users:????:Library:"
	read file "Macintosh HD:Users:????:Library:Application Support:tweetIndex_xednIteewt.txt"
		--> "2"
	path to frontmost application as string
		--> "Macintosh HD:Applications:Utilities:Script Editor.app:"
end tell
tell application "Script Editor"
	display dialog "fake tweet :\n#HappyDays more testing of desktop tweets\nto : fakeAccountID\n with picture : file1"
		--> {button returned:"OK"}
end tell
tell current application
	path to frontmost application as string
		--> "Macintosh HD:Applications:Utilities:Script Editor.app:"
end tell
tell application "Script Editor"
	display dialog "fake tweet :\nFollow the tweeting challenge\nto : fakeAccountID\n with picture : file2"
		--> {button returned:"OK"}
end tell
tell current application
	path to frontmost application as string
		--> "Macintosh HD:Applications:Utilities:Script Editor.app:"
end tell
tell application "Script Editor"
	display dialog "fake tweet :\nTesting Tweets from my desktop again\nto : fakeAccountID\n with picture : file3"
		--> {button returned:"OK"}
end tell
tell current application
	path to frontmost application as string
		--> "Macintosh HD:Applications:Utilities:Script Editor.app:"
end tell
tell application "Script Editor"
	display dialog "fake tweet :\n#HappyDays more testing of desktop tweets\nto : fakeAccountID\n with picture : file4"
		--> {button returned:"OK"}
end tell
tell current application
	path to frontmost application as string
		--> "Macintosh HD:Applications:Utilities:Script Editor.app:"
end tell
tell application "Script Editor"
	display dialog "fake tweet :\nFollow the tweeting challenge\nto : fakeAccountID\n with picture : file5"
		--> {button returned:"OK"}
end tell
tell current application
	path to frontmost application as string
		--> "Macintosh HD:Applications:Utilities:Script Editor.app:"
end tell
tell application "Script Editor"
	display dialog "currentIndex1 = 1"
		--> {button returned:"OK"}
end tell
tell current application
	open for access file "Macintosh HD:Users:????:Library:Application Support:tweetIndex_xednIteewt.txt" with write permission
		--> 114
	set eof 114 to 0
	write 1 to 114 starting at eof as text
	close access 114
end tell

Result:
true

Yes I did get the same behaviour without editing the post and I ran with 5 pictures. I have just tried again as asked and the tweets stop on the last in the list. What I did try was deleting the last tweet from the twitter feed and running again. This then posted the last tweet again! Hop that helps?

I tried the syntax above, and it posted the tweets, but not the images.

Not sure if this will help, as you may already be aware of it, but the code below works if the images are hard coded, as in the example below:



set imagePath to "HARD Disk X:Users:USERNAME:Pictures:TweetImages:test.jpg" 
	
	tweet "Tweet testing" using account "MyTwitterAccount" with image imagePath 


Thanks

Did a bit of cleaning

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) samedi 8 juillet 2017 15:53:19

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