How to make this run in background?

set the_list to ""
set nl to ASCII character 10
tell application "Finder"
	set file_list to name of every file of folder "Macintosh HD:users:dominus:documents:dosbox" whose name ends with ".conf"
	repeat with entry in file_list
		set the_list to the_list & nl & entry
	end repeat
end tell
set optionen to (choose from list file_list with prompt "DOSBox plays today")
if optionen is not false then
	do shell script "/Users/dominus/Code/cvs/dosbox/src/dosbox -conf /users/dominus/documents/dosbox/" & optionen as string & " &> /dev/null &"
else if optionen is false then
	do shell script "/Users/dominus/Code/cvs/dosbox/src/dosbox &> /dev/null &"
end if

What it does:
It displays all my config files for DOSBox stored in ~/documents/dosbox, when I choose one, it will start DOSBox with the parameter “-conf /users/dominus/documents/dosbox/name of the configfile”. Or if I don’t choose one, it will start DOSBox without a config file. All of this works.
What doesn’t work is the redirect to /dev/null when choosing a config file. AppleScript-Runner will not shut down until I close DOSBox.

“do shell script “/Users/dominus/Code/cvs/dosbox/src/dosbox &> /dev/null &”” does work, otoh…
I’m really stumped atm, and have no idea how to proceed…

P.S.: Sorry for the undescriptive subject, I couldn’t think of a good one…

Doesn’t anyone have an idea what is wrong here, or whether this will just not work the way it is set up?

Do you just want it to repeat forever until you tell it to stop?

No, I just want it to list all the files with the file ending .conf, then I choose one of those files and that file gets used in the line
do shell script “/Users/dominus/Code/cvs/dosbox/src/dosbox -conf /users/dominus/documents/dosbox/” & optionen as string & " &> /dev/null &"

so Dosbox gets started with that file as parameter. This is working so far, only I don’t want to have Apple-Script Runner still runnin gin the background as this prevents further scripts from running as long as this one does.
Normally this would be solved by adding the “&> /dev/null &” but that is not working in this case :frowning:

Is my problem with the repeat function?

Oh sorry, I skimmed the post, can’t help.

Do your filenames contain any shell-special characters?

If your chosen filename included a semicolon or an ampersand that would cause the shell to see two commands. The redirection and background-launch would only apply to the second one (the one that is not doxbox). A filename like “fun&games.conf” would result in a command line like “./dosbox -conf ./fun&games &> /dev/null &”, which the shell would interpret as two commands: “./dosbox -conf ./fun&” and “games &> /dev/null &”. But if that were the case, DOSBox would probably never load the proper config file since its name was truncated (though it might load some other config file if you have one called “fun”, or maybe “fun.conf” if DOXBox automatically adds the extension if it is not present).

Other special characters like spaces or tabs would prevent the full filename from being properly delivered to dosbox. But neither space nor tab would prevent the redirection and backgrounding from happening.

You should use quoted form of optionen as string instead of optionen as string in your do shell script string. That will make sure the proper filename gets to the program. Even if it does not solve your problem, you should always use quoted form of when passing string data via do shell script.

@Richard: Thanks anyway :slight_smile:

@Chrys: helpful advice, I will keep that in mind. It did not help with my problem, though :frowning:
Thanks!

Now I’m really stumped. I solved the problem, but I dodn’t know why…
this is the working code:

set the_list to ""
set nl to ASCII character 10
tell application "Finder"
	set file_list to name of every file of folder "Macintosh HD:users:dominus:documents:dosbox" whose name ends with ".conf"
	repeat with entry in file_list
		set the_list to the_list & nl & entry
	end repeat
end tell
set optionen to (choose from list file_list with prompt "DOSBox plays today")
set test to ((optionen as string) & "> /dev/null &")
if optionen is not false then
	do shell script "/Users/dominus/Code/cvs/dosbox/src/dosbox -conf /users/dominus/documents/dosbox/" & test & "> /dev/null &"
else if optionen is false then
	do shell script "/Users/dominus/Code/cvs/dosbox/src/dosbox &> /dev/null &"
end if

If I leave out & “> /dev/null &” in set test, it will not run in background. If I leave it out in the do shell script “…” & test ti will also not run in background.
(With background I mean that ApplScript Runner quits)
I don’t understand this but am glad that it works…

Edit: got it like this:


set test to ((optionen as string) & "&> /dev/null &")
if optionen is not false then
	do shell script "/Users/dominus/Code/cvs/dosbox/src/dosbox -conf /users/dominus/documents/dosbox/" & test

Not the extra & in “&> /dev/null &”

That extra ampersand was in your original post. What made it work was breaking it up into pieces, but only because of a side effect.
It turns out that your original code was being parsed in an unexpected way. You originally had something like this:

set optionen to {"something"} -- originally, the result of choose from list
do shell script "/tmp/test.sh " & optionen as string & " &> /dev/null &"

But the as is not being taken as a coercion, it is being used as the rarely used as parameter to do shell script. You can fix the original code by putting parentheses around the coercion (or by doing first item of optionen instead of the single item list to string coercion).

set optionen to {"something"} -- originally, the result of choose from list
do shell script "/tmp/test.sh " & (optionen as string) & " &> /dev/null &"
do shell script "/tmp/test.sh " & first item of optionen & " &> /dev/null &"

Because we do not see an " &> /dev/null &" at the end of the result of do shell script, we can surmise that the original code was not being parse as

(do shell script "/tmp/test.sh " & optionen as string) & " &> /dev/null &"

Instead it was parsed as

do shell script "/tmp/test.sh " & optionen as (string & " &> /dev/null &")

That ends up working like

do shell script "/tmp/test.sh " & optionen as { string, " &> /dev/null &"}

The final construct is obviously weird, but it is, unfortunately, not rejected by do shell script. Thus, the problem ended up being almost completely silent.

There might be a visual clue though. In my Script Editor, I have the formatting configured to show language keywords in bold and application keywords in normal weight (both are blue Veranda). The difference is that the original code formats without bold, indicating that the as is being taken as application syntax (i.e. part of do shell script). When the parentheses are wrapped around to force interpretation as a coercion, the as turns bold, indicating its use as an AppleScript keyword (the coercion operator).

So, new rule: When using explicit coercions in the arguments to do shell script, put them in parentheses.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4.0.4 (4531.21.10, r51280)
Operating System: Mac OS X (10.4)

Thanks for the explanation, after I wrapped my head around this, it makes perfect sense.
And double thanks for the very in depth explanation with examples. This kind of help is educating and next time I’ll really remember it :slight_smile: