Help Completing / Coding Droplet for Tweeting via Twitter Scripter

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

Testing on the active (test) twitter account gives the following error message - The variable indexForAccount is not defined. (-2753)

Oops. It’s corrected.
I read and reread before posting but nothing is more efficient that true execution to find oddities in a code.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 11 juillet 2017 14:55:46

More than happy to test and feedback. :slight_smile:

With howMany 2 - The following error appears. The variable otherAccount is not defined. (-2753)

With howMany 1 - The following error appears. The variable otherAccount2 is not defined. (-2753)

No tweets are posted in either case.

Oops, wording remaining from the time the tweets were issued in a handler.
What is odd is that I corrected the names in the code used for tests and forgot to do the same in the true code.
Maybe the retired potter must also retire as an helper.

The script is corrected in message #33

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 11 juillet 2017 15:53:24

Good news - No errors!

Bad news - No tweets! I am looking over why this is likely to be the case now. Any suggestions?

Thanks

[Updated]

Adding the below to the end of the code produces an error when run! Not sure if this helps.


Return tweeterAccount 

-- error "The variable tweeterAccount is not defined." number -2753 from "tweeterAccount"

I don’t know what you call “the end of the script”.
I edited the script in message #33.
Now it contain several instructions allowing it to create a log file on the desktop.
The file would be named : “log report_troper gol.txt”

Its contents is supposed to resemble to:
[format]twoAccounts = account1, account2

tweeterAccount : account1
tweeterAccount2 : account2

treat file : path2pict1
howmany : 1
theTweet : blahblah
tweeterAccount : account1
theTweet : baratin
tweeterAccount2 : account2

treat file : path2pict2
howmany : 1
theTweet : blahblah2
tweeterAccount : account1
theTweet : baratin2
tweeterAccount2 : account2

and so on.[/format]

If you look in the code you will see that I split the instruction setting the accounts into two instructions to be sure of what is returned.

I guess that other users aren’t interested by our exchanges, so if you wish, you may send the result to my mailbox :
k o e n i g . y v a n @ s f r . f r (without the spaces)

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

I duplicated the real script and disabled the calls to Twitter Scripter.
Everything behaved well.

The log text file contain:
[format]twoAccounts = account2, account3

tweeterAccount : account2
tweeterAccount2 : account3

treat file : file1
howmany : 1
theTweet : Testing Tweets from my desktop again
tweeterAccount : account2
theTweet : tweet 22
tweeterAccount2 : account3

treat file : file2
howmany : 1
theTweet : #HappyDays more testing of desktop tweets
tweeterAccount : account2
theTweet : tweet 23
tweeterAccount2 : account3[/format]

So it seems that the entire structure is OK.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 11 juillet 2017 18:56:29

[update at 18:34] I have the code working! I will post my changes later, as I have spotted another quirk.

When I tried to post a real tweet to the active account, which has been disabled it posted tweets from what seems to be a cache, as these were from code that had been replaced!

Hello Yvan,

I have spotted some anomalies with Twitter Scripter, as I tested it independently to your script.

1 - The image size must not be too large - this is known to be a problem with twitter, so not surprised
2 - You can’t post the same tweet twice! I.e. Tweet24 will only post once. If you change to Tweet24a it will post the tweet.

I ran your code and the log output for howMany 1 is:

The log output for howMany 3 is:

Using the code below, to test if the system was even working, I do get Tweets and Tweets with images. I have to change the text or image for it to post every time I run it though. Something you fixed in a previous script, as we did get the tweets cycling through the available list, and then restarting at the beginning.


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 3 of availableAccounts -- choose the right account!
	# 1 Should = Prescience
	# 2 Shoud = MainAccount
	# 3 Should = TestingAccount
	
	
	# tweet "I'm trying out #TwitterScripter from @mousedownsoft again." using account twitterAccount
	-- Works but does not post the same tweet twice! If you adjust by a character it does.  Could twitter handler be caching tweets?
	
	set imagePath to "Macintosh HD:Users:??????:Pictures:test2.jpg" -- replace with your own path
	
	tweet "#TwitterScripter #image size matters! By @mousedownsoft with an image defined in the above settings." using account twitterAccount with image imagePath
end tell
return twitterAccount
-- Displays in the result window the account set under 'item' from available accounts

-- Result window displays: TestingAccount

It would seem from the log that the outputs is exactly as desired, it would appear to be something with Twitter Scripter when it comes to posting the tweets. T

I will continue to test and investigate.

What are you dropping onto the script ?
You are supposed to drop a group of files. If you drop a folder the script will be unable to work because droppedFiles will be { theDroppedFolder as alias}.
The loop supposed to issue the tweets will try to create a tweet with the folder where it’s supposed to pass a file.

I edited the script in message #33 so that it creates the log file when you use it.
May you run it ?

As is, the new version has two entry points so, when it’s saved as an application, we may call it by a double click or by drag & drop.
We may also run it from the script editor so we may see the events log during the execution process.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 12 juillet 2017 12:17:18

May you look at : https://stackoverflow.com/questions/37394771/applescript-excel-2016-save-as-pdf ?
They posted :

on saveExcelAsPDF(documentPath, PDFPath) -- params = two HFS paths
    set tFile to (POSIX path of documentPath) as POSIX file -- get a posix file object to avoid grant access issue with 'Microsoft Office 2016',  not the same as (file documentPath) when using the 'open  ...' command

    tell application "Microsoft Excel"
        set isRun to running
        set wkbk1 to open workbook workbook file name tFile
        alias PDFPath -- This is necessary to any script for 'Microsoft Office 2016', this avoid errors with any "save ... " command
        save workbook as wkbk1 filename PDFPath file format PDF file format with overwrite
        close wkbk1 saving no
        if not isRun then quit
    end tell
end saveExcelAsPDF

The code may be modified to fit your needs.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) vendredi 14 juillet 2017 10:20:27