Copy a document relative

Uhm, I’m not sure if it’s possible.

I have a document text called “document” (without extension) inside the resource folder of droppled.
I need that when the script runs, it controls if a file name “document” exists in my preferences folder, and read it, if not exist, copy the the text file “document” nside the resource folder of droppled in my preferences folder.

tell application "Finder"
	if (exists document "Document" in preferences) then
		set x to read file "Document" in preferences from 1 to 7
	else
		copy file "Document" of (path to resources) to (path to preferences)
	end if
end tell

I know that there are many errors in this script… some one can help me, please?

Hi Matteo,

try this (I don’t understand what you mean with from 1 to 7)

set PrefFolder to path to preferences folder
set DocFile to (path to resource "Document")

tell application "Finder" to if (exists file "Document" in PrefFolder) then
	set x to read file ((PrefFolder as string) & "Document")
else
	tell application "Finder" to duplicate DocFile to PrefFolder
end if

Notes: if the resource “Document” doesn’t exist, the script will not compile.
path to resource doesn’t work, when the script will be started in Script Editor

That’s perfect!

Thank you very much!

Level 2 :lol:

I’ve edit your script so:

set PrefFolder to path to preferences folder
set DocFile to (path to resource "Document")

tell application "Finder" to if (exists file "Document" in PrefFolder) then
	display dialog "Yes, it exists" // this works only for take a look
	set x to read file ((PrefFolder as string) & "Document")
else
	display dialog "No files"
	tell application "Finder" to duplicate DocFile to PrefFolder
	set x to "8000"
end if

display dialog "x= " & x

if x is not 9000 then
	set box to display dialog "Type your code" default answer "" buttons {"Register", "Run demo"} default button 1
	(* Variabile a al testo inserito  *)
	set a to text returned of box
	(* Variabile b al bottone premuto  *)
	set b to button returned of box
	if b is "Register" then
		write a to file ((PrefFolder as string) & "Document")
	end if
else
	display dialog "registration is OK"
end if

The script run fine, but I have two problem.

  1. when i type “9000” in the box dialog, the variable x will be “9000”, but the
if x is not 9000 then

give me true: why?

  1. if i type “abcdefghi”, command “write” write “abcdefghi”. Then i type “9000” and command “write” write “9000”. So in the file I have “9000efghi”. How can I delete all before write?

you have once a string “9000” an then a number 9000, which is not the same

insert this line before writing

set eof f to 0 -- (f is the index number of the file)

uhm, point 2 is OK (tnx).
But the point 1 continue to give me a bad works (this script hates me, I know :frowning:

set PrefFolder to path to preferences folder
set DocFile to (path to resource "Document")

tell application "Finder" to if (exists file "Document" in PrefFolder) then
	display dialog "Yes, it exists"
	set x to read file ((PrefFolder as string) & "Document")
else
	display dialog "No files"
	tell application "Finder" to duplicate DocFile to PrefFolder
	set x to ""
end if

display dialog "x= " & x

set passw to "albero"

-- (here you are the problem)
if x is not equal to passw then
	set box to display dialog "Type your code" default answer "" buttons {"Register", "Run demo"} default button 1
	set a to text returned of box
	set b to button returned of box
	if b is "Register" then
		set eof file ((PrefFolder as string) & "Document") to 0 -- (f is the index number of the file)
		write a to file ((PrefFolder as string) & "Document")
	end if
else
	display dialog "registration is OK"
end if

Now, the password is “albero” (or something else, not important now).
If I open the file, i can see only albero inside, and the dialog window show me that x is “albero” (find “here you are the problem”)

But the

if x is not equal to passw

give me true (but is false): why?

The point is, the text written to the file is Unicode text,
but the read command without parameter reads normal text.

I recommend to use the common write routine

set PrefFolder to path to preferences folder
set DocFile to (path to resource "Document")
set PrefFile to (PrefFolder as string) & "Document"


tell application "Finder" to if (exists file "Document" in PrefFolder) then
	display dialog "Yes, it exists"
	set userPass to (read file PrefFile) as Unicode text -- !!
else
	display dialog "No files"
	tell application "Finder" to duplicate DocFile to PrefFolder
	set userPass to ""
end if

set passw to "albero"
-- (here you are the problem)
if userPass is not passw then
	set {a, b} to {text returned, button returned} of (display dialog "Type your code" default answer "" buttons {"Register", "Run demo"} default button 1)
	if b is "Register" then
		try
			set ff to open for access file PrefFile with write permission
			set eof of ff to 0
			write a to ff starting at eof
			close access ff
		on error
			try
				close access file PrefFile
			end try
		end try
	end if
else
	display dialog "registration is OK"
end if

I would guess that it’s not being read and/or written correctly.

Try using the user defaults system (via the defaults command line tool) instead:

tell userDefaults to readKey("RegistrationCode", "")

if result is not "albero" then
	display dialog "Enter your registration code:" default answer "" buttons {"Run Demo", "Register"} default button 2
	set {text returned:userInput, button returned:button} to result
	
	if button is "Register" then
		tell userDefaults to writeKey("RegistrationCode", userInput)
	end if
else
	display dialog "Registration is OK." buttons {"OK"} default button 1
end if


script userDefaults
	property _domain : "DefaultDomain" -- change this to something unique, like "matteo.discardi.YourApplicationName"
	
	on readKey(_key, _defaultValue)
		try
			do shell script "/usr/bin/defaults read " & quoted form of _domain & space & quoted form of _key
		on error
			return _defaultValue
		end try
	end readKey
	
	on writeKey(_key, _value)
		do shell script "/usr/bin/defaults write " & quoted form of _domain & space & quoted form of _key & space & quoted form of _value
	end writeKey
end script

uh… uhm…
Tnx to everyone. I have to ceck all script, study them and… a lot to learn.

more then a lot, I think.
But thank you very much.

I hope, in the future, to be so usefull as you to other users.

bye