Why does script not work when run from under Applescript Menu?

I have some code that works perfectly when run from the Applescript Editor. When run by selecting it from under the Applescript symbol in the active window menu, it does not.

The script does the following:

  1. Obtains text from the clipboard.
  2. Processes the text.
  3. Pastes the text into Excel.

Your insight would be greatly appreciated.
:slight_smile:

Would be easier to answer knowing your script, at least its skeleton.
Knowing which is the running OS may be useful too.

Yvan KOENIG (VALLAURIS, France) mercredi 4 décembre 2013 19:10:02

Merci beaucoup monsieur. He said in his best 7 year old French–my age when my family left France. :wink:

Here is the code. Thank you so much for going over it.


set dailyValues to the clipboard as text

tell application "Finder" to activate

set outputValues to "" as text
set dailyValuesList to {}
set dailyValuesList to my getListFromText(return, dailyValues)

repeat with aDaysValues in dailyValuesList
	set daysValuesList to {}
	set daysValuesList to my getListFromText(tab, aDaysValues)
	set outputValues to outputValues & item 1 of daysValuesList & tab & item 2 of daysValuesList & tab & item 5 of daysValuesList & tab & item 8 of daysValuesList & tab & item 11 of daysValuesList & tab & item 14 of daysValuesList & return as text
end repeat

set the clipboard to outputValues

tell application "Microsoft Excel"
	activate
	set dataRange to used range of active sheet
	set theRows to count of every row of used range of active sheet
	set saveRow to (theRows + 2)
	activate object cell saveRow of column 1
	set theRange to active cell
	paste worksheet active sheet destination theRange
	set pastedRange to current region of active cell
	sort pastedRange key1 (ranges (row 1 of pastedRange) thru (last row of pastedRange)) order1 sort ascending
	copy range pastedRange
end tell

on getListFromText(theSeparator, theTextList)
	set theResult to {}
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theSeparator}
	set theResult to text items of theTextList
	set AppleScript's text item delimiters to atid
	return theResult
end getListFromText

Hello.

I tried your script after I had started up Excel, and seen to that I had an empty workbook that was open.
I also assured that there were enough elements in the rows, so that the items could be extracted from the list.

Your script ran flawlessly.

I convoluted your script with an error handler, and a handler that writes to a log file, nothing showed up.

I am running Excel 2004 under Mavericks.

Suggestions:

Count the number of elements in the rows, after you have extracted them.

Maybe check if Excel is running, and your workbook of choice is the frontmost, (that it indeed is a workboook there.)

(You can should be able to see the log with the name you decided upon in Console.app.)

to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

Thank you very much for the feedback!!! Your log script is going to help me a great deal in the future!

Thanks again! :smiley:

Hello

Here is a slightly edited version.


on run
	local dailyValues, outputValues, dailyValuesList, aDaysValues, daysValuesList, outputValues, theRows, saverow, therange, pastedRange
	set dailyValues to the clipboard as text
	
	# tell application "Finder" to activate # USELESS
	
	set outputValues to "" # as text  # USELESS
	# set dailyValuesList to {}  # USELESS
	set dailyValuesList to paragraphs of dailyValues # my getListFromText(return, dailyValues)
	
	repeat with aDaysValues in dailyValuesList
		# set daysValuesList to {}  # USELESS
		set daysValuesList to my getListFromText(tab, aDaysValues)
		set outputValues to outputValues & item 1 of daysValuesList & tab & item 2 of daysValuesList & tab & item 5 of daysValuesList & tab & item 8 of daysValuesList & tab & item 11 of daysValuesList & tab & item 14 of daysValuesList & return #as text
	end repeat
	
	set the clipboard to outputValues
	set dailyValues to ""
	set aDaysValues to ""
	set dailyValuesList to {}
	set outputValues to ""
	
	tell application "Microsoft Excel"
		activate
		# set dataRange to used range of active sheet  # USELESS
		set theRows to count of every row of used range of active sheet
		set saverow to (theRows + 2)
		activate object cell saverow of column 1
		set therange to active cell
		paste worksheet active sheet destination therange
		delay 0.2 # maybe it must be greater
		set pastedRange to current region of active cell
		sort pastedRange key1 (ranges (row 1 of pastedRange) thru (last row of pastedRange)) order1 sort ascending
		copy range pastedRange
	end tell
end run

on getListFromText(theSeparator, theTextList)
	local theResult
	# set theResult to {}  # USELESS
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theSeparator}
	set theResult to text items of theTextList
	set AppleScript's text item delimiters to atid
	return theResult
end getListFromText

I can’t test it because I don’t own a running version of Excel. I just own an old try version which can’t be used but whose dictionary is used by AppleScript editor.

Yvan KOENIG (VALLAURIS, France) mercredi 4 décembre 2013 21:18:46