Using Applescript to make a script and add to login items.

Hey all,

I am trying to make a script that prepares a script to mount a folder for users and places it in their login items. Any help is greatly appreciated! Im stuck on the saving part. Also, trying to figure out how to stop if user says No. I’m new at this.

--Find UserName
set userName to do shell script "whoami"
--Make ScanFolder Directory
set part1 to "mkdir /Users/" & userName & "/scanfolder"
--Ask User for Building
--display dialog part1
set building to choose from list {"BB", "Houses","PS", "Science"} with prompt "Please select your building:" default items {"BB"}
--Build Script
set part2 to "mount_smbfs  //'ad;" & userName & "@server/scans/" & building & "/" & userName & "' /users/" & userName & "/scanfolder"
--display dialog part2
set dialog to "Are you sure you want to create the scan directory with building " & building & "?"
--try
--	tell application "Finder"
--		set answer to display dialog dialog buttons {"Yes", "No"} default button 1
--	end tell
--	if answer = "Yes" then
tell application "AppleScript Editor"
	launch
	activate
	make new document with properties {contents:part2}
	save document 1 in "Macintosh HD:MountScanFolder" as application
end tell
--end if
--on error
--	set answer to {button returned:"No"}
--end try

Hi RayK. Welcome to MacScripter.

The immediate answer to your question about the saving part is that you need a name for the script applet and that has to be included in the save path. (You save a script to a file, not a folder.) Also, “application” in the ‘save’ line should be text, not a keyword.

save document 1 in file ((path to startup disk as text) & "MountScanFolder:Fred.app") as "application"

It may be possible to do it without AppleScript Editor, but I’ll have to get back to you about that.

Edit: PS. The text of the script has to be a representation of a “do shell script” command, with your part2 quoted as text within it:

make new document with properties {contents:"do shell script \"" & part2 & "\""}
save document 1 in file ((path to startup disk as text) & "MountScanFolder:Fred.app") as "application"

Hi,

your script won’t compile, because the do shell script command is missing in front of the line.
The script can be compiled without opening AppleScript Editor using osacompile.

As Nigel wrote, you have to specify a filename, the script uses Login.app

Pressing the No button aborts the script


--Find UserName
set userName to do shell script "whoami"
--Make ScanFolder Directory
set part1 to "mkdir /Users/" & userName & "/scanfolder"
--Ask User for Building
--display dialog part1
set building to choose from list {"BB", "Houses", "PS", "Science"} with prompt "Please select your building:" default items {"BB"}
--Build Script
set part2 to "do shell script \"mount_smbfs  //'ad;" & userName & "@server/scans/" & building & "/" & userName & "' /users/" & userName & "/scanfolder\""
--display dialog part2
set dialog to "Are you sure you want to create the scan directory with building " & building & "?"

tell application "Finder"
	activate
	display dialog dialog buttons {"Yes", "No"} default button 1 cancel button "No"
end tell

-- compile the script as application. The folder MountScanFolder must exist
do shell script "osacompile -e " & quoted form of part2 & " -o /MountScanFolder/Login.app"

-- add Login.app to login items 
tell application "System Events"
	make new login item at end of login items with properties {path:"/MountScanFolder/Login.app", hidden:false}
end tell