Welcome to MacForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

please help: probably small problem with my if statement

 
   Macintosh computer (Home) -> Apple Scripts RSS
Next:  Entertainment: Experience Blu-ray discs and movie..  
Author Message
Pepa

External


Since: Mar 18, 2010
Posts: 1



(Msg. 1) Posted: Thu Mar 18, 2010 7:01 am
Post subject: please help: probably small problem with my if statement
Archived from groups: alt>comp>lang>applescript (more info?)

Hi guys,

I have a perfectly working applecsript for TextWrangler (below: all
the stuff between the first else and last end if) and only wanted to
make it activate textwrangler if i work in another application; if i'm
in textwrangler i want it to run the rest of the script. however, when
i try to run this the applescript editor gives me an error complaining
that "Expected “end” but found “on”." referring to the "on
ComputeTempFileName(originalFile)" line.

I'm new to this... and none of what i tried worked.

Would you have any idea how to fix this? thanks a lot. Josef

if name of current application is not "textwrangler"
tell application "textwrangler" to activate
else
tell application "TextWrangler"
set thecontents to the selection in window 1 as string
set theOriginalFile to file of document 1
if thecontents is "" then
tell text window 1
select line (startLine of selection)
end tell
end if
set thecontents to the selection in window 1 as string
set theOriginalFile to file of document 1
end tell
set thecontents to thecontents & return & "erase temp.do" & return &
"exit"
set tempFile to my ComputeTempFileName(theOriginalFile)
my write_to_file(thecontents, tempFile, false)
tell application "Finder" to set filename to tempFile as alias
tell application "StataSE"
activate
get name
end tell
if name of application "StataSE" = "StataSE" then

tell application "System Events"
tell process "StataSE"
set frontmost to true
tell window 1
get name
end tell
if name of window 1 = "data editor" then

tell window "data editor"
keystroke "w" using command down
end tell
end if
end tell
end tell
tell application "StataSE"
activate
open filename
end tell
end if
on ComputeTempFileName(originalFile)
tell application "Finder"
set c to container of originalFile
set t to (c as string) & "temp.do"
end tell
return t
end ComputeTempFileName

on write_to_file(this_data, target_file, append_data)
tell application "Finder"
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end tell
end write_to_file
end if

 >> Stay informed about: please help: probably small problem with my if statement 
Back to top
Login to vote
Jolly Roger

External


Since: Sep 09, 2006
Posts: 3131



(Msg. 2) Posted: Thu Mar 18, 2010 11:07 am
Post subject: Re: please help: probably small problem with my if statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article
,
Pepa wrote:

> Hi guys,
>
> I have a perfectly working applecsript for TextWrangler (below: all
> the stuff between the first else and last end if) and only wanted to
> make it activate textwrangler if i work in another application; if i'm
> in textwrangler i want it to run the rest of the script. however, when
> i try to run this the applescript editor gives me an error complaining
> that "Expected łend˛ but found łon˛." referring to the "on
> ComputeTempFileName(originalFile)" line.
>
> I'm new to this... and none of what i tried worked.
>
> Would you have any idea how to fix this? thanks a lot. Josef
>
> if name of current application is not "textwrangler"
> tell application "textwrangler" to activate
> else
(snip)
> on ComputeTempFileName(originalFile)
> tell application "Finder"
> set c to container of originalFile
> set t to (c as string) & "temp.do"
> end tell
> return t
> end ComputeTempFileName
>
> on write_to_file(this_data, target_file, append_data)
> tell application "Finder"
> try
> set the target_file to the target_file as text
> set the open_target_file to ¬
> open for access file target_file with write permission
> if append_data is false then ¬
> set eof of the open_target_file to 0
> write this_data to the open_target_file starting at eof
> close access the open_target_file
> return true
> on error
> try
> close access file target_file
> end try
> return false
> end try
> end tell
> end write_to_file
> end if

"ComputeTempFileName" and "write_to_file" are handlers (equivalent to
subroutines in other programing languages). You can't place handlers
inside of if blocks like that. So move the "end if" statement at the
very end of the script up to just under where I wrote "(snip)" above.

It seems all your script does is:

1. copy a single line from a text file that happens to be open in
TextWrangler

2. write that line to a text file, adding two more lines: "erase
temp.do" and "exit"

3. close the data editor window

4. instruct StataSE to open the file

Is the line of text in question one that must be located (selected)
manually, or might you be able to compute that? If the latter, you could
do all of this without involving TextWrangler at all (just read the text
file in, find the line you want, and continue).

--
Send responses to the relevant news group rather than email to me.
E-mail sent to this address may be devoured by my very hungry SPAM
filter. Due to Google's refusal to prevent spammers from posting
messages through their servers, I often ignore posts from Google
Groups. Use a real news client if you want me to see your posts.

JR

 >> Stay informed about: please help: probably small problem with my if statement 
Back to top
Login to vote
Dave Balderstone

External


Since: Mar 21, 2006
Posts: 1675



(Msg. 3) Posted: Thu Mar 18, 2010 3:15 pm
Post subject: Re: please help: probably small problem with my if statement [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

Back to top
Login to vote
Jolly Roger

External


Since: Sep 09, 2006
Posts: 3131



(Msg. 4) Posted: Thu Mar 18, 2010 8:19 pm
Post subject: Re: please help: probably small problem with my if statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article ,
Dave Balderstone wrote:

> In article ,
> Jolly Roger wrote:
>
> > You can't place handlers
> > inside of if blocks like that. So move the "end if" statement at the
> > very end of the script up to just under where I wrote "(snip)" above.
>
> Good catch, JR.

Thanks, Dave. It had me perplexed for a few minutes, until I realized
what was going on.

--
Send responses to the relevant news group rather than email to me.
E-mail sent to this address may be devoured by my very hungry SPAM
filter. Due to Google's refusal to prevent spammers from posting
messages through their servers, I often ignore posts from Google
Groups. Use a real news client if you want me to see your posts.

JR
 >> Stay informed about: please help: probably small problem with my if statement 
Back to top
Login to vote
Display posts from previous:   
   Macintosh computer (Home) -> Apple Scripts All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]