Page 1 of 1

Set button label from external

Posted: Sat Jun 20, 2009 10:17 am
by bidgeeman
Hi.
I'v been stuck on this one for a while.
I'm trying to set button labels from text lines of an external text file.

Code: Select all

on mouseUp
   answer folder "choose folder"
   if it is empty then exit mouseUp
   put it into tTitles
   open url tTitles , "/Test.txt"
    set the label of button "Button2" to line 1 of url tTitles , "/Test.txt"
end mouseUp
If anyone could offer a tip as to where I'm falling over it would be much appreciated. :) :oops:

Thanks
Bidge

Posted: Sat Jun 20, 2009 11:05 am
by gyroscope
Hi Bidge

your first line is incorrect: instead of

Code: Select all

answer folder "choose folder"
try:

Code: Select all

answer file "choose folder"
(or with a bit of refinement):

Code: Select all

answer file "choose file:"
Also here's your revised code for what you want to achieve:

Code: Select all

on mouseUp
    answer file "choose file:" 
   if it is empty then exit mouseUp 
  put url ("file:"&it) into tData
    
    set the label of button "Button2" to line 1 of tData
end mouseUp
It seems to work...

:)

Posted: Sat Jun 20, 2009 11:16 am
by bidgeeman
Hi Gyro.

Thanks for the response. I'm loading other files from that folder, hence the line

" open url tTitles , "/Test.txt".

The file is Test.txt located in the same folder and want to put the first few lines of that txt file into the label of some buttons.
Even with that script change you suggested it did'nt work so I must be doing something else wrong in the script???

Cheers
Bidge

Posted: Sat Jun 20, 2009 3:31 pm
by BvG
you are mixing file-access methods. "open file" can't be used with the url keyword (and is too complex to be used in 99% of all cases).

what you want is probably something like this:

Code: Select all

on mouseUp
  answer folder ""
  if it = "" or the result = "cancel" then
    exit mouseUp
  end if
  put url ("file:" & it & "/test.txt") into contentOfFile
  set the label of button 1 to lie 1 of contentOfFile

Posted: Sun Jun 21, 2009 12:10 am
by bidgeeman
Thanks for pointing that out BvG.

Code: Select all

put url ("file:" & it & "/test.txt") into contentOfFile 
This file access method is new to me (I'm just learning). Many thanks for the help. :)

Cheers
Bidge
[/quote]