Copy Files inside Application

Hi Folks,

may you can help me with the following:

in my application I have 3 text files - and one destination file:

apn_at.txt
apn_de.txt
apn_uk.txt

if a “if Clause” is true the file p.ex. apn_at.txt has to copied to the destination file: apn_mac.txt

here is my code so far:


	-- Imsi holen
	
	repeat
		set xstr to "at+CIMI" & return
		serialport write xstr to portRef
		delay 1
		set imsi to serialport read portRef
		display dialog imsi
		delay 1
		set imsi to characters 11 thru 15 of imsi as string
		
		
		display dialog imsi
		
		if imsi is "23203" then
			display dialog "T-Mobile A"
			set apncheck to "1"
			exit repeat
		else if imsi is "26201" then
			display dialog "T-Mobile D"
			set apncheck to "2"
			exit repeat
		else if imsi is "23432" then
			display dialog "T-Mobile UK"
			set apncheck to "3"
			exit repeat
		end if
	end repeat
	
	-- Land setzen:
	
	set fileToCheck to ((path to me as Unicode text) & "Contents:Resources:apn_mac.txt")
	
	repeat
		tell application "Finder" to set nofile to (not (exists file fileToCheck))
		
		tell application "Finder" to set yesfile to (exists file fileToCheck)
		if yesfile then exit repeat
		
		if nofile then
			display dialog "hier"
			display dialog apncheck
			try
				if apncheck is "1" then
					
					display dialog "da"
					set dest to ((path to me as Unicode text) & "Contents:Resources:apn_at.txt")
					set source to ((path to me as Unicode text) & "Contents:Resources:apn_mac.txt")
					
					do shell script "cp dest source"
					set run_file to "1"
					exit repeat
				else if apncheck is "2" then
					tell application "Finder"
						duplicate alias ((path to me as Unicode text) & "Contents:Resources:apn_de.txt") to alias ((path to me as Unicode text) & "Contents:Resources:apn_mac.txt")
					end tell
					exit repeat
				else if apncheck is "3" then
					tell application "Finder"
						duplicate alias ((path to me as Unicode text) & "Contents:Resources:apn_uk.txt") to alias ((path to me as Unicode text) & "Contents:Resources:apn_mac.txt")
					end tell
					exit repeat
				end if
			end try
		end if
		
		
		if run_file is "1" then exit repeat
		
	end repeat
	
	--APN holen
	
	delete every menu item of menu of popup button "apnlist" of drawer "Setting" of window "main"
	
	set file_path to ((path to me as string) & "Contents:Resources:apn_mac.txt") as file specification
	set read_data to paragraphs of (read file_path)
	repeat with i from 1 to (length of read_data)
		set current_item to (item i of read_data) as string
		make new menu item at end of menu of popup button "apnlist" of drawer "Setting" of window "main" with properties {title:current_item, enabled:true}
	end repeat
	
	set apn_file to ((path to me as Unicode text) & "Contents:Resources:apn.txt") as file specification
	set apn to read apn_file as string
	
	set contents of text field "useapn" of drawer "Setting" of window "main" to apn


May you can help me solving this issue, because in this code the duplication will not work…

It is also funny, that even if apncheck is “1” I am not able to get out of the Repeat Loop…

Thanks for any help!

Stefan

Hi Stefan,

the duplicate command of the Finder cannot duplicate and rename files at the same time
An easier way is to use the shell, I guess the destinaton file “apn_mac.txt” should be replaced always
Here a suggestion to shorten the code (assuming the value of apncheck is always only “1”, “2” or “3” ;))

.
if nofile then
		-- display dialog "hier"
		-- display dialog apncheck
		set resourceFolder to ((path to me as Unicode text) & "Contents:Resources:")
		set source to quoted form of POSIX path of (resourceFolder & item (apncheck as integer) of {"apn_at.txt", "apn_de.txt", "apn_uk.txt"})
		set destination to quoted form of POSIX path of (resourceFolder & " apn_mac.txt")
		try
			do shell script "cp -f " & source & " " & destination
			exit repeat
		end try
	end if
.

PS: I can’t remember, if source is a reserved keyword in AppleScript Studio, so use something else

Hi Stefan,

thanks for your help - it works like a charme - ok - there is a problem
while copying - because as the file apn_mac.txt is not available, I will get the following
messages:


if nofile then
			
			try
				if apncheck is "1" then
					
					set resourceFolder to ((path to me as Unicode text) & "Contents:Resources:")
					set sourceFolder to quoted form of POSIX path of (resourceFolder & item (apncheck as integer) of {"apn_at.txt", "apn_de.txt", "apn_uk.txt"})
					display dialog sourceFolder
					set destinationFolder to quoted form of POSIX path of (resourceFolder & " apn_mac.txt")
					display dialog destinationFolder
					try
						set x to do shell script "cp -f " & sourceFolder & " " & destinationFolder
						display dialog x
						exit repeat
					end try
				else if apncheck is "2" then
					
				else if apncheck is "3" then
				end if
			end try
		end if


display dialog x is presenting “”

Error Message: File file Macintosh HD:Users:sl:Documents:Web’n’walk Manager Version 3.0:Web’n’walk Manager MacOS TMA:build:Release:Web’n’walk Manager MacOS.app:Contents:Resources:apn_mac.txt wasn’t found. (-43)

Thanks for your help!

BR

Stefan

sorry, remove the space character in " apn_mac.txt"

set destinationFolder to quoted form of POSIX path of (resourceFolder & "apn_mac.txt")

Hi Stefan,

thanks a lot - but x is still empty and I get a

AS Error (-39) - End of file error…

Thanks for your help,

BR

Stefan

I believe cp doesn’t have any output by default. For testing purposes, you might ask for more verbose output.

set x to do shell script "cp -fv " & sourceFolder & " " & destinationFolder

I tried this code with an AppleScript application bundle and it works without any problem

Hi Stefan, Hi Bruce,

it is funny, but it seems that I am not able to copy the file…

I have tried on my Intel and on my PPC…

Do I need special permissions?

Does the File has to be existent?

BR

Stefan

Of course the source file must exist

Hi Stefan, hi Bruce,

I have to appologize! It was my error! The “end file error” occours because of an empty
file which I was using later in the script…

BR

Stefan