Friday, November 16, 2018

#1 2018-03-10 03:41:38 am

ldicroce
Member
Registered: 2017-11-25
Posts: 70

Call a subroutine with the Tell

Hi all,
I have the following script structure:

Applescript:

tell application "Finder"
-- some commands here
-- from I want to call a subroutine:
write_text_to_file(the_Temp_path, file_data) --- this line give me an error
end tell

on write_text_to_file(the_file, the_text)
   set file_ref to open for access the_file with write permission
   set eof file_ref to 0
   write the_text to file_ref as «class utf8»
   close access file_ref
end write_text_to_file

The only way I found to avoid an error is to use:
tell me to write_text_to_file(the_Temp_path, file_data)

Is the best way? Or there are alternatives?
Thanks a lot

Offline

 

#2 2018-03-10 03:48:13 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11519
Website

Re: Call a subroutine with the Tell

ldicroce wrote:



Is the best way?



It's the way.

Alternative syntax is

Applescript:

my write_text_to_file(the_Temp_path, file_data)

and

Applescript:

write_text_to_file(the_Temp_path, file_data) of me

but all forms do the same thing.


regards

Stefan

Offline

 

#3 2018-03-10 03:50:57 am

ldicroce
Member
Registered: 2017-11-25
Posts: 70

Re: Call a subroutine with the Tell

Thanks a lot!

Offline

 

#4 2018-11-07 07:58:01 am

Adak47
Member
Registered: 2018-08-18
Posts: 7

Re: Call a subroutine with the Tell

ldicroce wrote:

The only way I found to avoid an error is to use:
tell me to write_text_to_file(the_Temp_path, file_data)

Is the best way? Or there are alternatives?
Thanks a lot

Can't you just move the tell application "Finder" block to the subroutine? Then the call in the main routine should work just as you had it originally (I think). What causes the error with your original script is that by being imbedded in the tell application "Finder" block the statement write_text_to_file(the_Temp_path, file_data) is being interpreted by the Finder, which doesn't understand it. Or am I missing something? (I am a noob to AppleScripting.)

If the Finder tell block needs to be in the main routine because of the other commands (not shown), then you could just move the write_text_to_file statement down one line to pull it out of the Finder tell block, since it isn't intended for the Finder anyway.

Last edited by Adak47 (2018-11-07 08:07:23 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)