Creating a script from text

k…i’ve been searching for the answer to this question for a day and a half and can’t find anything. i know it’s possible to create a text file using an applescript. but, is it possible to create a new script (.scpt) file using a script? here’s what i want to do. i need to make a script which either modifies or makes a completely new script based both on set parameters and user input. the user input supplies information needed to mount a windows share on a computer with a previously undetermined computer name.

here’s what i have so far:

set compName to text returned of (display dialog “Enter your Computer Name.” & return & return & “e.g., Workstation” default answer “” buttons {“Cancel”, “OK”} default button 2 with icon 1)

set finalString to “mount volume smb://username:password@” & compName & “/shareName”

set targetFile to “:volumes:workstationx:users:jason:desktop:starting.scpt”

set targetFile to open for access file targetFile with write permission

write finalString to targetFile

close access targetFile

this results in a file named “starting.scpt” on my desktop. but, the file is inaccessible by Script Editor. i can open it in TextEdit and the command is correct (mount volume “smb://username:password@compName/sharename”). i’ve tried using the same setup script to changed the file type and creator to osas and ToyS respectively. still won’t open in Script Editor and i’m unable to get it to function as a script.

where am i goin’ wrong? thanks

Well, several places. First, you have to coerce your string to a script. Even if you did that, you would fail because your string would not compile because it isn’t properly quoted. If you did coerce to a script, then you save it by using the “store script” command, not by using the file read/write commands:

set targetFile to (":volumes:workstationx:users:jason:desktop:starting.app" as file specification)
set compName to text returned of (display dialog "Enter your Computer Name." & return & return & "e.g., Workstation" default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1)
set finalString to "mount volume 'smb://username:password@' & compName & '/shareName'"
do shell script "osacompile -e " & (quoted form of finalString) & " -o " & (quoted form of POSIX path of (targetFile as string)) & " -t " & (quoted form of "APPL") & " -c " & (quoted form of "aplt") & " -x"

Jon

don’t know why i didn’t think of that. oh wait…it’s 'cause i’m not a master like Jon…:slight_smile:

thanks again

You’re welcome. I forgot to mention that if you are using a scriptable script editor such as Script Editor 2.0, you could also do something like this:

set targetFile to ":volumes:workstationx:users:jason:desktop:starting.scpt" as file specification
set compName to text returned of (display dialog "Enter your Computer Name." & return & return & "e.g., Workstation" default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1)
set finalString to "mount volume 'smb://username:password@' & compName & '/shareName'"

tell application "Script Editor"
	make new document at beginning
	set text of document 1 to finalString
	check syntax of document 1
	save document 1 as "script" in targetFile with run only
	close document 1 saving no
end tell

Jon