Re: Creating new file etc
Re: Creating new file etc
- Subject: Re: Creating new file etc
- From: kai <email@hidden>
- Date: Fri, 13 Apr 2007 10:05:17 +0100
On 13 Apr 2007, at 04:15, Gregory White wrote:
I'm trying to create a new file in the given directory and then
replace old data with new data should that be required.
I will get that data from the clipboard as it originates in another
app. The data will be text.
Hi, Gregory.
There's just a tiny flaw in your script that's causing it to fail.
The first line of your 'write_to_file' handler specifies the
parameters of the subroutine - including the variable 'target_file'.
This contains the value from the variable 'this_file' - which was
originally defined in (and is local to) the calling routine. So far
so good.
However, the first line within the subroutine's try statement then
attempts to explicitly reassign the value of 'this_file', which is
beyond the scope of the subroutine, to 'target_file'. This results in
an error [number -2753: OSAUndefinedVariable] - "The variable
this_file is not defined." (Since the error is trapped by the error
handler, the message is not returned.)
The fix is quite simple: remove the redundant assignment statement.
-------------------
tell application "Finder"
copy (the clipboard) to Clip_Board
end tell
set Bookmaker_Folder to "home:Documents:ReferenceWorks Bookmaker:"
--in the finished script I would set this_book to Clip_Board
set this_Book to "Once upon a time in Silicon Valley... Hello"
set this_file to (((Bookmaker_Folder) as text) & "New Book")
-- in the above assignment statement, you may also want to consider
adding a name extension to the file. For example:
-- set this_file to Bookmaker_Folder & "New Book.txt" (* the
parentheses and coercion are probably unnecessary *)
my write_to_file(this_Book, this_file, false)
on write_to_file(this_data, target_file, append_data)
try
-- ******** delete the following assignment statement ******** --
-- set the target_file to the this_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 write_to_file
-------------------
Apologies for merely suggesting a couple of tweaks. Just couldn't
manage a complete rewrite right now...
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden