-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_disk_image.scpt
179 lines (151 loc) · 7.41 KB
/
make_disk_image.scpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Set constants
# Initialize the script
######
# Welcome the user
set dialogText to "This script will create a usable disk image containing your AppleSoft BASIC program(s). You will be guided through the process. Your BASIC program(s) need to be saved as text file(s) before you start. Press OK to continue."
display dialog dialogText buttons {"OK"} default button "OK"
# Check Java installation
on noJavaError()
set errorText to "The script encountered a problem with your Java installation. Please ensure java is installed and available at the command line. The easiest way to install java is from the java website, which will open upon exit"
display dialog errorText buttons {"Exit"}
do shell script "open https://www.java.com/en/download/"
quit
end noJavaError
try
do shell script "java -h"
on error
noJavaError()
end try
# Get info from the user about what they want to do and create base image
######
# Prompt the user for output folder
set outFolder to choose folder with prompt "Please select a destination folder for your disk image"
# Prompt the user for output file name
display dialog "Please enter a name for your disk image. This name cannot contain spaces or special characters" default answer "my_image" with icon note buttons {"Cancel", "Continue"} default button "Continue"
set outImageName to text returned of the result
# Prompt the user for the base image
set dialogText to "Select your base disk image.\n\nEmpty images do not contain DOS 3.3 so DOS must be loaded from another disk prior to running programs contained on the result.\n\nBootable images contain DOS 3.3 and have less space for larger programs, but allow you to boot and run from a single disk."
set imageChoice to the button returned of (display dialog dialogText buttons {"Empty Image", "Bootable Image"} default button "Bootable Image")
# Get path to input reference file for use in the next step
# this is a relative path to this applescript, since they should stay in the same folder
on getInputImagePath(imageChc)
set DOS_MASTER_RELPATH to "/disk_images/reference/dos_3_3_master.dsk"
set EMPTY_RELPATH to "/disk_images/reference/dos140_empty.dsk"
set thisPath to POSIX path of (path to me)
set workingPath to do shell script "dirname " & thisPath
if (imageChc = "Empty Image") then
set imagePath to workingPath & EMPTY_RELPATH
else if (imageChc = "Bootable Image") then
set imagePath to workingPath & DOS_MASTER_RELPATH
end if
return imagePath
end getInputImagePath
set inputImagePath to (POSIX file getInputImagePath(imageChoice) as alias)
# TODO ^ change these to Resources once packaged as app i.e.
# set resourceName to "blablabla.dsk"
# set filePathName to quoted form of POSIX path of (path to resource resourceName) as text
# checks if a file exists in a folder, if so adds a _1 (etc) so we dont hit rename collisions
on createNewFileName(fileLocation, fileName, fileExtension)
set x to 1
if fileExtension = null or fileExtension = "" then
set fileExtension to ""
else
set fileExtension to "." & fileExtension
end if
set existingFiles to list folder fileLocation with invisibles
set availableFileName to fileName & fileExtension
repeat
if availableFileName is in existingFiles then
set availableFileName to fileName & "_" & x & fileExtension
set x to x + 1
else
return availableFileName
end if
end repeat
end createNewFileName
# Copy input image to destination for upcoming modification
on createTargetImage(baseImagePath, outFileName, outFolder)
set outFileNameNR to createNewFileName(outFolder, outFileName, "dsk")
tell application "Finder"
set duplicatedFile to duplicate file baseImagePath to folder outFolder with replacing
set name of duplicatedFile to outFileNameNR
end tell
return alias ((outFolder as text) & outFileNameNR)
end createTargetImage
set newTargetImage to createTargetImage(inputImagePath, outImageName, outFolder)
# Add each input BASIC file to the disk image
######
# Gets the path to the applecommander executable
on getACPath()
set AC_RELPATH to "/applecommander/AppleCommander-ac-1.8.0.jar"
set thisPath to POSIX path of (path to me)
set workingPath to do shell script "dirname " & thisPath
set workingPathAlias to POSIX FILE (workingPath & AC_RELPATH) as alias
return workingPathAlias
end getACPath
# Makes the AppleCommander Command to run
on makeACCommand(basicFilePath, programName, targetImagePath)
set acPath to getACPath()
set theCommand to "cat " & (POSIX path of basicFilePath)
set theCommand to theCommand & " | java -jar " & (POSIX path of acPath)
set theCommand to theCommand & " -bas " & (POSIX path of targetImagePath)
set theCommand to theCommand & " " & programName
log theCommand
return theCommand
end makeACCommand
# Called if AppleCommander hits an error adding the basic file
on acJavaError(errMsg)
set acMsg to "There was an error adding your basic file to the disk.\nHave you confirmed it's a valid integer basic file?\nDetails below:\n\n" & errMsg
display dialog acMsg
quit
end acJavaError
# Adds an applesoft BASIC file to a target image
on addFileToImage(basicFilePath, programName, targetImagePath)
set acCommand to makeACCommand(basicFilePath, programName, targetImagePath)
try
do shell script acCommand
on error errMsg
acJavaError(errMsg)
end try
end addFileToImage
# Gets a suggested name for a basic program.
# e.g. by default, "myprog.bas" converts to MYPROG
on getSuggestedName(basicFilePath)
set theCommand to "basename " & (POSIX path of basicFilePath)
set theCommand to theCommand & " | sed 's/\\(.*\\)\\..*/\\1/'"
set theCommand to theCommand & " | sed 's/[^[:alnum:]-]//g'"
set theCommand to theCommand & " | tr [:lower:] [:upper:]"
set parsedName to do shell script theCommand
return parsedName
end getSuggestedName
# TODO ^add resource paths instead of using relative path here.
set addBasic to true
repeat while (addBasic = true)
# Get program file
set basicToAdd to choose file with prompt "Select your Apple basic program file (.txt / .bas)"
set suggestedName to getSuggestedName(basicToAdd)
# Get program name
display dialog "Please enter a name for your program. This name cannot contain spaces or special characters" default answer suggestedName with icon note buttons {"Cancel", "Continue"} default button "Continue"
set addAsName to text returned of the result
# Add them to the image
set cmdResult to addFileToImage(basicToAdd, addAsName, newTargetImage)
# See if the user wants to add more files, if so repeat
display dialog "Would you like to add any more BASIC files to your disk?" buttons {"Yes", "No"} default button "No"
set doContinue to the button returned of the result
if (doContinue = "No")
set addBasic to False
end if
end repeat
# Generates the shutdown dialog with helpful information
on generateFinalDialog(targetImageLocation)
set finalDialog to "Complete - Your image is now ready to use!\n\n"
set finalDialog to finalDialog & "Your file was saved as: \n"
set finalDialog to finalDialog & (POSIX path of targetImageLocation) & "\n\n"
set finalDialog to finalDialog & "Click OK to open the file in Finder and exit"
display dialog finalDialog
end generateFinalDialog
# Finish up
generateFinalDialog(newTargetImage)
tell application "Finder"
reveal newTargetImage
end tell