one script to call another

I have this:


tell application "checkTxtAndLogOutput"
	run
end tell

which works, but it runs the code twice not once as i hoped that it would.

Anyideas?

Is checkTxtAndLogOutput an Applescript? Could you post the script so we could see?

I can’t reproduce your problem. I created a simple display dialog script (application) for testing and ran this:

tell application "external script test"
	run
end tell

The external script executed only once but the calling script did not quit running. So I tried this:

tell application "external script test"
	ignoring application responses
		run
	end ignoring
end tell

It worked. The external script executed only once and the calling script quit after sending the run command. I suspect that your checkTxtAndLogOutput script needs help. It sounds like a handler is being executed twice.

– Rob

here is the code, best bet is to copy it into your scripter, as i have quite a few variables.


set footerMessage to "This information was automatically generated.  The files that have already been uploaded have been moved to the trash, the files that are shown in the log aren't uploaded and haven't been moved. " & return & "Copyright BlueGreenPictures, for information please email us at: " & return & "info@bluegreenpictures.com" & return & "-----------------" & return & return
----Th eabove message is a disclaimer, not really needed but makes the report look more professional

--set the alias for the files and folders
set the_log_file to "Shared G4 HD:Users:karrenmay:Desktop:libraryLog.txt" as alias
set the_file to "Shared G4 HD:Users:karrenmay:Desktop:lbraryno.txt" as alias -- a text file with the file names each on a seperate line, one per line 
set all_folders to {"Shared G4 HD:Users:karrenmay:Desktop:web pics:"}
--counters for the footer
set notCount to "0"
set delCount to "0"
set listCount to "0"
---------------
--------------------------------------------------------
-- This checks a folders files to see if they have been uploaded
-- If the file han't been then it will send the filename toa log file
-- Created by Andrew Davey with help from macscripter.net
--------------------------------------------------------

set list_from_file to read the_file as list using delimiter return
set listCount to (count of list_from_file)


repeat with j from 1 to (count of all_folders)
	set the_folder to (item j of all_folders) as alias
	set list_from_folder to list folder the_folder without invisibles
	
	repeat with i from 1 to (count of list_from_folder)
		
		set the_item to (item i of list_from_folder) as string
		if list_from_file does not contain the_item then
			
			my add_item_to_file(the_log_file, the_item)
			--set not coount to +1 so that it can be used as an already uploaded counter
			set notCount to (notCount + 1)
		else
			my removeTheFile((the_folder as string) & the_item)
			--set the delcount to +1 as a counter to see how many deleted
			set delCount to (delCount + 1)
		end if
	end repeat
	activate
	display dialog "Files scanned: " & listCount & return & "Files already uploaded: " & delCount & return & "Files not uploaded: " & notCount & return & return & "The files already uploaded have been moved to the trash" buttons {"OK"}
	--add the footer at the end of the document
	my addFooter(the_log_file, delCount, notCount, footerMessage, listCount)
end repeat
---------------------------------------------------------------------------
-- Add the filename to the log file, starting at the end of file
---------------------------------------------------------------------------
on add_item_to_file(the_file, the_item)
	try
		open for access the_file with write permission
		write (the_item & "   --->   hasn't been uploaded" & return) to the_file starting at eof
		close access the_file
	on error
		try
			close access the_file
		end try
	end try
end add_item_to_file
---------------------------------------------------------------------------
-- Remove the file and move it to the trash
---------------------------------------------------------------------------
on removeTheFile(the_item)
	try
		tell application "Finder" to delete (the_item as alias)
	on error
		display dialog "Could not delete " & the_item & ""
	end try
end removeTheFile

--method to add a footer at the end of the document as a seperator and report
on addFooter(the_file, delCount, notCount, footerMessage, listCount)
	try
		set numberChecked to (delCount + notCount)
		open for access the_file with write permission
		write (return & " --------------------- " & return & "Number of files uploaded so far: " & listCount & return & "Number of Files Checked: " & numberChecked & return & "Number Already Uploaded: " & delCount & return & "Number Not Uploaded: " & notCount & return & " --------------------- " & return & footerMessage) to the_file starting at eof
		close access the_file
	on error
		try
			close access the_file
		end try
	end try
	
end addFooter

set list_from_file to ""
set list_from_folder to {}

Also on the side, i would like to get the script to on each run empty the txt file and then writ ethe log.

How can i do this.

Perhaps make the main body of code a function as well, and then do

tell myScript
doMain
end tell

To clear the file, first open it and then

set eof FileReference to 0

That will reset the end of file pointer to position zero, making the file empty.

Alternatively you could simply move the file to the trash and make a new one each time, but setting eof is probably easier (by one line of code)

I don’t see anything that would trigger the script twice. How is the script that runs this script activated? Is it a folder action?

This handler (untested) should empty a text file.

on empty_text_file(path_to_text_file)
	set f to open for access file path_to_text_file with write permission
	try
		set eof of f to 0
		close access f
	on error
		try
			close access f
		end try
	end try
end empty_text_file

I was always under the impression that a file needed to be opened with write permission to do this but a quick test reveals that this is not true. Is this an accepted way to eof a file?

– Rob

It was just as i called it above, just to test and see a way to do it.

Thanks for the eof, just gonna see if it works.

have tried what you posted rob, but no luck. I don’t know why.

Hmm… I’ll work it out.

I tested it after posting the code and it worked.

From the AppleScriptLanguageGuide.pdf, the tell statement sends an implicit run to the script app. In pre-OSX the script used to hang on the second (explicit?) run command. On my comp it errors on the explicit run command.
To stop this use the ‘launch’ command:

tell app “TestScript”
launch
run
end tell

I didn’t try the ‘ignoring app response’ with this but if you do it with the ‘launch’, then you get a result.

gl,

I’ve been having a similar problem so I was considering posting, then I saw your response.

This illustrates why I am so frustrated with Applescript: IT’S SO FRAGILE!

When I do this:
tell app “TestScript”
launch
run
end tell

It works about half the time.
If I insert a DELAY 1 after the LAUNCH, it works every time.

Without the delay, I see TWO run events (sometimes more) in the event log, but the sub-script app never launches (the finder icon for it is busy).
It waits and waits, then gives a timeout.

If I command-period while it’s hung waiting, I see one or two or THREE more run events sent - still no launch.

But it works every time with the delay.

— Well, I spoke too soon.
While I was experimenting with the DELAY 1, I got it down to delay 0.1 and it was working. Just for grins, I commented out the delay command, and it would work only about half the time. As I expected from earlier tests.

So I put it back in, and now it fails sporadically. On my event log now, I see that it sent not one, not two, not three, but SIX run events to the sub-script ???

I have to make this bulletproof.

The only reason I need to launch one script from another is that I need to get a path to a folder that’s alongside the script. The PATH OF ME construct fails when in the editor. So I wrote a script app to take PATH OF ME and strip off the last item. As a compiled script it works OK. I just cann’t seem to call it reliably.