-- WPDOSBox - by Edward Mendelson, 2 March 2013

-- This is the main.scpt in an application bundle

-- containing DOSBox and configuration files 

-- for WPDOS 5.1 and 6.2, with an option to run Shell 3.1 or 4.0

-- starting December 2010 includes patched DOSBox

-- modified 29 Aug 2011 for Lion, 17 October with new macros, patches

-- modified 16 September 2012 for options setting

-- modified 28 October 2012 to simplify options

-- folder action script modified 3 Feb 2013 to add host commands

-- modified 29 March 2013 to warn about MacCopy etc. macros

-- todo: add routines for Arabic and Hebrew WPDOS 5.1 - framework only, so far


global myPlist, noFilePrompt, runOnceFlag, myName

global dosFldr, dosExe, dosApp, dosPath, confFile, confAlias, confPath, userHomePath, exeFldr

global procName, myName, wpVer, userLibPath, filesPath, aliasFile, diskPath, docsFolder, wpFldr

global thisApp, filesFolder, runAlready, testFileName, shellwp, Hebrew, Arabic

global sharedSetPath, sharedSetPosix, tempConfPath, tempConfPosix

global formerFnKeyState, resetFnKeysPosix, doDrop, editDir, setEnvPath, setEnvFilename

global infileName, nameOnly, parentDirOnly

----------------------------------------------------

-- variables for installing printing system:

global userPath, libPath, userLibScrPath, userFldrPath, thisApp, diskAlias

global printAction, printActionAlias, doPrompt, doSetup, sysAbsent, printPath, silentSetup

global printActionAlias

global printFldrExists, printActionPosix

-------------------------------------

property msgTitle : "WPDOS under DOSBox"

property fldrScript : "DOSBoxConvertPrint.scpt"

property showHelp : false

----------------------------------------

property macTypeList : {"WPD1", "WPD2", "WPD3"} -- filetypes for WPMac documents

property macEnhancedTypeList : {"WPD4"} -- filetype for WPMac35e

property dropName : ""

property fixedName : ""

property infileName : ""

property dropItem : ""


property oldFileDate : ""

property newFileDate : ""

property firstRun : false


-- noFilePrompt - if set to "true", the application will not prompt when

-- a folder or file is dropped on it; "false" enables the explanatory prompt

property noFilePrompt : "false"


-- fullScreen - this option opens DOSBox in full-screen mode, not

-- in a window; Cmd-Enter toggles between full-screen mode and windowed

-- mode exactly as it does when DOSBox opens in windowed mode

property fullScreen : "false"

-- add "FS" to the name of this application to make this true, or use 

-- option key to set options


-- useCustomBuild - This option, when set to "true", uses a "custom build" of

-- DOSBox created for this system. This custom build includes improved support

-- for 512-character fonts and monochrome mode, and uses better-looking screen

-- fonts. It also includes other minor improvements, but may have bugs and other

-- problems that are not present in the release version.

property useCustomBuild : "true"


-- use25Line - this system defaults to opening DOSBox in a  43-line DOS window.

-- To restore the normal 25-line screen, set this option to "true" 

-- or add the string "VGA" to the name of the application

property use25Line : "false" -- change to "true" to force 25-line mode 


-- useOpenGL - if you find that graphic modes are too slow under 10.7

-- then change the useOpenGL setting to "true" (with quotation marks).

-- Text modes will be off-center on screen, but graphics may be faster.

property useOpenGL : "false"


-- fixFnkeys - if this option is set to "true" the function keys on a Macintosh

-- keyboard will be changed (if necessary) so that it will not be necessary to

-- hold down the "fn" key to access (for example) F7, and the Fn keys will not

-- perform the purpose engraved on the key itself (such as previous track, etc.).

-- If you have already set the Fn keys to work as standard function keys 

-- (using the setting in System Preferences, Keyboard) this option will

-- have no effect. This option takes effect only under OS X 10.6 or later.

property fixFnKeys : "true"


property shellwp : "no" -- change to "three" or "four" to force starting in Shell 3.1 or 4.0c

property wpVer : "51" -- change to "62" to force WPDOS 6.2, otherwise wpVer set by filename

property doMacroWarning : "true" -- posts warning if needed under WPDOS 5.1


to open (theDrop)

set doDrop to true

tell application "System Events" to set procName to item 1 of (get name of processes whose frontmost is true)

getWPVersion()

readPList()

getMyName()

getEditDir(theDrop)

checkKeyboardState() -- leads to setOptions()

setupPaths()

makePrintSystem()

addPlugins()

if fixFnKeys is "true" then

set formerFnKeyState to getFnKeyState()

end if

runApp()

quit

error number -128

end open


to run

set doDrop to false

tell application "System Events" to set procName to item 1 of (get name of processes whose frontmost is true)

getWPVersion()

readPList()

getMyName()

checkKeyboardState() -- leads to setOptions()

setupPaths()

makePrintSystem()

addPlugins()

if fixFnKeys is "true" then

set formerFnKeyState to getFnKeyState()

end if

runApp()

quit

error number -128

end run


on quit

tell application "System Events" to tell process procName to set myPID to unix id

do shell script "kill -9 " & myPID

end quit


on getWPVersion()

set myPlist to "org.wpdos.wpdosbox"

tell application "System Events" to set myName to name of (get properties of (path to me))

set myName to text 1 thru -5 of myName

set wpVer to 0

if myName contains "5" then

set wpVer to 51

set myPlist to "org.wpdos.wpdosbox51"

else if myName contains "6" then

set wpVer to 62

set myPlist to "org.wpdos.wpdosbox62"

end if

end getWPVersion


on getMyName()

-- set myPlist to "org.wpdos.wpdosbox"

-- tell application "System Events" to set myName to name of (get properties of (path to me))

-- set myName to text 1 thru -5 of myName

if myName contains "help" then

set showHelp to true

end if

if myName contains "Shell" then

if myName contains "Shell3" then

set shellwp to "three"

else if myName contains "Shell4" then

set shellwp to "four"

else

tell me to activate

set dialogText to "Add either “Shell3” or “Shell4” to my name to launch the WordPerfect Office Shell." & return & return & "Quitting."

my errorQuit(dialogText)

end if

end if

if myName contains "Hebrew" then

set Hebrew to true

else if myName contains "Arabic" then

set Arabic to true

end if

-- set use25Line to "false"

if myName contains "VGA" then

set use25Line to "true"

end if

if myName contains "FS" then

set fullScreen to "true"

end if

end getMyName


on setupPaths()

set thisApp to path to me as text

set filesFolder to (thisApp & "Contents:Resources:Files:") as text

set filesFolder to filesFolder as alias

set checkKeysPath to (thisApp & "Contents:Resources:Files:checkModifierKeys")

set checkKeysPosix to quoted form of (POSIX path of checkKeysPath)

set userPath to path to home folder as text

set userLibPath to path to library folder from user domain as text

set dosFldr to (path to me) & "Contents:Resources:Files:" as string

if useOpenGL is "false" then

set sharedSetPath to (path to me) & "Contents:Resources:Files:sharedsettings.conf" as string

else

set sharedSetPath to (path to me) & "Contents:Resources:Files:sharedsettings-opengl.conf" as string

end if

set sharedSetPosix to quoted form of (POSIX path of sharedSetPath)

set tempConfPath to (path to me) & "Contents:Resources:Files:dosboxtemp.conf" as string

set tempConfPosix to quoted form of (POSIX path of tempConfPath)

set resetFnKeysPath to (path to me) & "Contents:Resources:Files:ResetFnKeysAfterDOSBox.app" as string

set resetFnKeysPosix to quoted form of (POSIX path of resetFnKeysPath)

set exeFldr to dosFldr & "DOSBox.app:Contents:MacOS:" as string

set dosExe to exeFldr & "DOSBox" as string

set dosApp to dosExe as alias

set dosPath to (POSIX path of dosApp)

set userHomePath to path to home folder as text

try

set diskAlias to userHomePath & "DOSDisk" as alias

on error

set dialogText to "A folder named “DOSDisk” must be in your home folder, and must contain the WordPerfect for DOS program files."

errorQuit(dialogText)

end try

set diskPath to (POSIX path of diskAlias)

set setEnvPath to quoted form of (diskPath & "/UTIL/SETENV.BAT")

set setEnvFilename to (userHomePath & "DOSDisk:UTIL:SETENV.BAT") as string

set shiftState to "0"

-- set cmdState to "0"

-- set optionState to "0"

set shiftState to do shell script checkKeysPosix & space & "shift"

-- set cmdState to do shell script checkKeysPosix & space & "cmd"

-- set optionState to do shell script checkKeysPosix & space & "option"

if shiftState is "1" then

if wpVer is 51 then

set wpVer to 62

else

if wpVer is 62 then

set wpVer to 51

end if

end if

end if

set confString to "DOS.prof"

if wpVer is 51 then

try

set wpFldr to userHomePath & "DOSDisk:WP51" as alias

on error

set dialogText to "A folder named WP51 must be in your DOSDisk folder, and must contain the WordPerfect 5.1 for DOS program files."

errorQuit(dialogText)

end try

try

set wpExe to userHomePath & "DOSDisk:WP51:wp.exe" as alias

set wpFil to userHomePath & "DOSDisk:WP51:wp.fil" as alias

on error

set dialogText to "The WordPerfect program files must be present in the WP51 folder in your DOSDisk folder."

errorQuit(dialogText)

end try

if doMacroWarning is "true" then

set wpDate to (creation date of (info for wpExe))

if wpDate is less than date "Saturday, January 1, 1994 12:00:00 AM" then

tell me to activate

display dialog "Your version of WordPerfect 5.1 is too old to support the MacCopy and MacPaste macros supplied with this system." & return & return & "If you wish to use these macros, please install WordPerfect 5.1 Plus." & return & return & "This message will not appear again." buttons {"OK"} with title msgTitle

do shell script "defaults write org.wpdos.wpdosbox51 DoMacroWarning false"

-- else

-- tell me to activate

-- display dialog "It's OK"

end if

end if

set confString to "WPDOS51.prof"

else

if wpVer is 62 then

try

set wpFldr to userHomePath & "DOSDisk:WP62" as alias

on error

set dialogText to "A folder named WP62 must be in your DOSDisk folder, and must contain the WordPerfect 6.2 for DOS program files."

errorQuit(dialogText)

end try

try

set wpExe to userHomePath & "DOSDisk:WP62:wp.exe" as alias

set wpFil to userHomePath & "DOSDisk:WP62:wp.fil" as alias

on error

set dialogText to "The WordPerfect program files must be present in the WP62 folder in your DOSDisk folder"

errorQuit(dialogText)

end try

set confString to "WPDOS62.prof"

end if

end if

if shellwp is "three" then

set confString to "Shell31.prof"

else if shellwp is "four" then

set confString to "Shell40.prof"

end if

set confFile to dosFldr & confString as string

set confAlias to confFile as alias

set confPath to (POSIX path of confAlias)

set docsFolder to POSIX path of diskPath & "WPDocs/"

set tempFolder to POSIX path of diskPath & "Temp/"

try

POSIX file tempFolder as alias

on error

do shell script "/bin/mkdir -p" & space & quoted form of (diskPath & "/Temp")

end try

try

POSIX file docsFolder as alias

on error

do shell script "/bin/mkdir -p" & space & quoted form of (diskPath & "/WPDocs")

POSIX file docsFolder as alias

end try

set aliasFile to ((path to desktop as text) & "WPDocs")

tell application "Finder"

if exists file aliasFile then

else

if firstRun is true then

make new alias file at desktop to POSIX file docsFolder

-- do shell script "echo AlreadyRunOnce > " & (quoted form of POSIX path of filesFolder) & testFileName

end if

end if

end tell

end setupPaths


on makePrintSystem()

systemExists()

if sysAbsent = "yes" then

askToSetup()

if doSetup = "yes" then

setupPrintSys()

end if

end if

end makePrintSystem


on runApp()

try

do shell script "rm " & quoted form of diskPath & "/Printout/Temp/*"

end try

try

do shell script "rm " & quoted form of diskPath & "/Printout/*"

end try

try

do shell script "rm " & docsFolder & ".DS_Store"

end try

if useCustomBuild is "true" then

if use25Line is "true" then

set dosPath to dosPath & "-25line-snow"

else

set dosPath to dosPath & "-patched-snow"

end if

end if

set theText to "@echo off

REM Do not modify this file. It is re-created automatically." as text

if doDrop is true then

set theText to theText & "

Mount Y: \"" & editDir & "/\"

Y:

set infile=Y:\\" & infileName & "

rem" as text

end if

set myFile to (open for access (file setEnvFilename) with write permission)

set eof myFile to 0 -- clear out any existing contents

write theText to myFile

close access myFile

if fullScreen is "true" then

set setScreenPath to (path to me) & "Contents:Resources:Files:setfullscreen.conf" as string

else -- if fullScreen is false then

set setScreenPath to (path to me) & "Contents:Resources:Files:setwindowmode.conf" as string

end if

set setScreenPosix to quoted form of (POSIX path of setScreenPath)

set setTempPath to (path to me) & "Contents:Resources:Files:settemp.conf" as string

set setTempPosix to quoted form of (POSIX path of setTempPath)

do shell script "cat " & sharedSetPosix & space & setScreenPosix & " >" & setTempPosix

do shell script "cat " & setTempPosix & space & quoted form of confPath & " >" & tempConfPosix

do shell script "rm " & setTempPosix

do shell script quoted form of dosPath & " -conf " & tempConfPosix & " > /dev/null 2>&1 &"

if fixFnKeys is "true" then

if formerFnKeyState is "1" then

else

set dosRuns to false

repeat until dosRuns = true

tell application "System Events"

set dosRuns to (application process "DOSBox" exists) -- returns "true" if running

delay 0.1

end tell

end repeat

do shell script "open " & resetFnKeysPosix & " &"

end if

end if

end runApp


on systemExists()

set sysAbsent to "no"

set userPath to path to home folder as text

set libPath to path to library folder from user domain as text

set printPath to (diskAlias & "Printout") as text

set printFldrExists to "yes"

try

set printAlias to printPath as alias

on error

set sysAbsent to "yes"

set printFldrExists to "no"

end try

set userLibScrPath to path to scripts folder from user domain

try

set userLibScrAlias to userLibScrPath as alias

on error

set sysAbsent to "yes"

end try

try

set userFldrPath to path to Folder Action scripts

on error

set sysAbsent to "yes"

end try

try

set userFldrAlias to userFldrPath as alias

on error

set sysAbsent to "yes"

end try

set actionScript to (userFldrPath & fldrScript) as text

try

set actionAlias to actionScript as alias

on error

set sysAbsent to "yes"

end try

if sysAbsent is "no" then

my setPrintActionPaths()

-- set oldFileDate to creation date of (info for actionAlias)

-- set newFileDate to size of (info for printActionAlias)

set oldFileDate to date "Friday, February 1, 2013 12:00:00 AM"

if (creation date of (info for actionAlias)) comes before oldFileDate then

-- if oldFileDate is not equal to newFileDate then

try

do shell script "cp " & printActionPosix & " ~/Library/Scripts/" & quote & "Folder Action Scripts" & quote

on error

tell me to activate

display dialog "Couldn't update the Folder Action Script to the latest version. Unless you use the advanced option to print to a PCL printer, this will not affect you." buttons {"Continue"} default button 1 with icon caution with title msgTitle giving up after 20

end try

end if

end if

if printFldrExists = "yes" then

tell application "System Events"

launch

set folder actions enabled to true

set curScripts to (get attached scripts of folder printPath)

if curScripts is {} or item 1 of curScripts is not (actionScript as alias) then

set sysAbsent to "yes"

end if

end tell

end if

return sysAbsent

end systemExists


on askToSetup()

my setPrintActionPaths()

set dialogText to "Do you want to set up a system that can automatically create PDF files from WordPerfect or print from WordPerfect to the default OS X printer?" & return & return & "You only need to perform this setup once."

tell me to activate

display dialog dialogText buttons {"Yes", "No"} default button 1 with icon caution with title msgTitle

set userChoice to button returned of result

if userChoice = "Yes" then

set doSetup to "yes"

end if

if userChoice = "No" then

set doSetup to "no"

end if

return doSetup

end askToSetup


on setupPrintSys()

-- install output folder, copy script file

do shell script "/bin/mkdir -p " & quoted form of diskPath & "/Printout"

set printPath to (diskPath & "Printout")

do shell script "/bin/cp " & quoted form of POSIX path of printAction & space & quoted form of POSIX path of userFldrPath

set actionScript to (userFldrPath as text) & fldrScript

--process folder action

tell application "System Events"

launch

set folder actions enabled to true

set curScripts to (get attached scripts of folder printPath)

if curScripts is {} or item 1 of curScripts is not (actionScript as alias) then

try

attach action to folder printPath using actionScript

-- on error e

-- tell me to activate

-- display dialog "Error attaching folder action script:" & return & e

end try

else if curScripts is not {} and item 1 of curScripts is (actionScript as alias) then

tell folder action "Printout"

set enabled to true

set enabled of script fldrScript to true

end tell

end if

end tell

tell me to activate

display dialog "Printing system installed." with title msgTitle buttons {"OK"} default button 1 giving up after 5

end setupPrintSys


-------------------------------------------------------------

-- subroutine for adding Spotlight and QuickLook filters


on addPlugins()

set userLibAlias to userLibPath as alias

set sysVers to ((system attribute "sysv") mod 4096 div 16)

set sysLibPath to path to library folder from local domain as text

set sysLibAlias to sysLibPath as alias

set spotFile to (path to me) & "Contents:Resources:Files:WordPerfect.mdimporter" as string

set spotFile to spotFile as alias

set quickFile to (path to me) & "Contents:Resources:Files:WordPerfect.qlgenerator" as string

set quickFile to quickFile as alias

set oldFileDate to date "Wednesday, February 1, 2012 12:00:00 AM"

try

set sysSpotFolder to sysLibPath & "Spotlight" as alias

end try

set spotFound to true

try

set sysSpotFolder to sysLibPath & "Spotlight" as alias

on error

set spotFound to false

end try

set wpSpotFound to false

if spotFound is true then

set sysSpotPath to sysSpotFolder as text

set sysWPSpot to sysSpotPath & "WordPerfect.mdimporter"

tell application "System Events"

if exists file sysWPSpot then

if creation date of file sysWPSpot comes after oldFileDate then

set wpSpotFound to true

end if

end if

end tell

end if

if wpSpotFound is false then

set userSpotFolderFound to true

try

set userSpotFolder to (userLibPath & "Spotlight") as alias

on error

set userSpotFolderFound to false

end try

if userSpotFolderFound is true then

set userSpotPath to userSpotFolder as text

set userWPSpot to userSpotPath & "WordPerfect.mdimporter"

tell application "System Events"

if exists file userWPSpot then

if creation date of file userWPSpot comes after oldFileDate then

set wpSpotFound to true

end if

end if

end tell

end if

end if

if wpSpotFound is false then

tell me to activate

display dialog "I will now install (or update) the WordPerfect Spotlight filter by Gero Herrmann." & return & return & "The Finder may prompt you for your password." buttons {"OK"} with title msgTitle giving up after 5

-- try to copy file to system folder, if fails, to user folder

try

tell application "Finder"

set copy_file to duplicate spotFile to folder sysSpotFolder with replacing

end tell

on error

tell application "Finder"

try

make new folder at folder userLibAlias with properties {name:"Spotlight"}

end try

set userSpotFolder to userLibPath & "Spotlight" as alias

try

set copy_file to duplicate spotFile to folder userSpotFolder with replacing

end try

end tell

end try

end if

try

set sysQuickFolder to sysLibPath & "QuickLook" as alias

end try

set QuickFound to true

try

set sysQuickFolder to sysLibPath & "QuickLook" as alias

on error

set QuickFound to false

end try

set wpQuickFound to false

if QuickFound is true then

set sysQuickPath to sysQuickFolder as text

set sysWPQuick to sysQuickPath & "WordPerfect.qlgenerator"

tell application "System Events"

set wpQuickFound to false

if exists file sysWPQuick then

if creation date of file sysWPQuick comes after oldFileDate then

set wpQuickFound to true

end if

end if

end tell

end if

if wpQuickFound is false then

set userQuickFolderFound to true

try

set userQuickFolder to userLibPath & "QuickLook" as alias

on error

set userQuickFolderFound to false

end try

if userQuickFolderFound is true then

set userQuickPath to userQuickFolder as text

set userWPQuick to userQuickPath & "WordPerfect.qlgenerator"

tell application "System Events"

set wpQuickFound to false

if exists file userWPQuick then

if creation date of file userWPQuick comes after oldFileDate then

set wpQuickFound to true

end if

end if

end tell

end if

end if

if wpQuickFound is false then

tell me to activate

display dialog "I will now install (or update) the WordPerfect QuickLook filter by Gero Herrmann." & return & return & "The Finder may prompt you for your password." buttons {"OK"} with title msgTitle giving up after 5

try

tell application "Finder"

set copy_file to duplicate quickFile to folder sysQuickFolder with replacing

end tell

on error

tell application "Finder"

try

make new folder at folder userLibAlias with properties {name:"QuickLook"}

end try

set userQuickFolder to userLibPath & "QuickLook" as alias

try

set copy_file to duplicate quickFile to folder userQuickFolder with replacing

end try

end tell

end try

end if

end addPlugins


on setPrintActionPaths()

set userPath to path to home folder as text

set libPath to path to library folder from user domain as text

try

do shell script "mkdir ~/Library/Scripts"

end try

try

do shell script "mkdir ~/Library/Scripts/" & quote & "Folder Action Scripts" & quote

end try

set userLibScrPath to path to scripts folder from user domain with folder creation

set userFldrPath to path to Folder Action scripts with folder creation

set thisApp to path to me as text

set filesFolder to (thisApp & "Contents:Resources:Files:") as text

set filesFolder to filesFolder as alias

set printAction to (filesFolder & fldrScript) as text

set printActionAlias to printAction as alias

-- set newFileSize to size of (info for printActionAlias)

set printActionPosix to quoted form of (POSIX path of printActionAlias)

end setPrintActionPaths


on getFnKeyState()

try

set formerFnKeyState to do shell script "defaults read -g com.apple.keyboard.fnState"

on error

set formerFnKeyState to "0"

end try

if formerFnKeyState is "0" then

tell application "System Events"

set prefsRunning to (name of processes) contains "System Preferences" -- returns "true" if running

end tell

tell application "System Preferences"

if prefsRunning is true then

set currentPane to current pane

set flag to true

try

currentPane -- errors if current pane is empty

on error

set flag to false

end try

end if

reveal anchor 1 of pane id "com.apple.preference.keyboard" -- anchor 1 is anchor "KeyboardTab"

end tell

tell application "System Events"

if UI elements enabled then

tell application process "System Preferences"

-- checkbox 1 below is checkbox "Use all F1, F2, etc. keys as standard function keys" 

-- window 1 below is window "Keyboard"

if value of checkbox 1 of tab group 1 of window 1 is 0 then

--set formerFnKeyState to 0

click checkbox 1 of tab group 1 of window 1

end if

end tell

tell application "System Preferences"

if prefsRunning is false then

quit

else

if flag then

if currentPane is pane id "com.apple.preference.keyboard" then

set show all to true

end if

set current pane to currentPane

else

set show all to true

end if

end if

end tell

--return formerFnKeyState

else

-- GUI scripting not enabled; display an alert

tell me to activate

display dialog "To make the function keys accessible to WordPerfect (without pressing the fn key) I must turn on the OS X feature “Enable access for assistive devices”." & return & return & "May I turn on this feature?" & return & return & "If you answer Yes, OS X will prompt you for a password." buttons {"No", "Yes"} default button 2 with icon caution with title msgTitle

set enableGUI to button returned of result

if enableGUI is "No" then

tell me to activate

display dialog "You may enable the required feature in System Preferences, in the Universal Access pane, by adding a checkbox next to  “Enable access for assistive devices”." & return & return & "If for some reason you do not want to make this change, you may disable this warning by editing the Applescript of this application and setting the option “fixFnKeys” to “false”. " & return & return & "This application will now close." buttons {"OK"} default button 1 with icon caution with title msgTitle

quit

error number -128

else

tell application "System Events" to set UI elements enabled to true

my getFnKeyState()

end if

end if

end tell

end if

return formerFnKeyState

end getFnKeyState


on cautionQuit(dialogText) -- Only a caution, possibly quit?

tell me to activate

display dialog dialogText buttons {"Continue?", "Quit"} default button 2 with icon caution with title msgTitle

set userChoice to button returned of result

if userChoice = "Quit" then

error number -128

end if

end cautionQuit


on errorQuit(dialogText) -- Fatal error, quitting script

tell me to activate

display dialog dialogText buttons {"OK"} default button 1 with icon stop with title msgTitle giving up after 5

error number -128

end errorQuit


on getEditDir(theDrop)

if wpVer is 51 or wpVer is 62 then

if (count items of theDrop) > 1 then

tell me to activate

display dialog "Please drop only one file or folder on me. I can do nothing with more than one." buttons {"OK"} default button 1 with icon stop with title msgTitle giving up after 10

error number -128

end if

set dropItem to (item 1 of theDrop)

set dropInfo to info for dropItem

if folder of dropInfo then

set editDir to getFolderPath(dropItem)

if noFilePrompt is "false" then

tell me to activate

display dialog "When DOSBox launches, I will set the current directory in WordPerfect for DOS to be this folder:" & return & return & editDir & return & return & "This message closes automatically, or you may click OK." buttons {"OK"} default button 1 with title msgTitle giving up after 5 -- getFolderPath(item 1 of theDrop) -- name of dropInfo 

end if

else

set editDir to getParentPath(dropItem)

set infileName to getInfileName(dropItem)

------

if noFilePrompt is "false" then

if parentDirOnly is "no" then

display dialog "When DOSBox launches, I will open “" & the name of dropInfo & "” and set the current directory in WordPerfect for DOS to be its containing folder: " & return & return & editDir buttons {"OK"} default button 1 with title msgTitle giving up after 10 -- getParentPath(item 1 of theDrop)

end if

end if

-----

end if

return editDir

else

set doDrop to false

set editDir to ""

tell me to activate

display dialog "I can only use a dropped file or folder if I am set to open WordPerfect automatically." & return & return & editDir buttons {"OK"} default button 1 with title msgTitle giving up after 15

end if

end getEditDir


on getFolderPath(theFile)

tell application "Finder" to return the POSIX path of theFile as text

end getFolderPath


on getParentPath(theFile)

tell application "Finder"

set parentPath to the container of theFile as alias

return the POSIX path of parentPath

end tell

end getParentPath


on getInfileName(dropItem)

set parentDirOnly to "no"

set getInfo to info for dropItem

set dropName to the name of getInfo

if the length of dropName is greater than 12 then

my reportBadFile()

end if

if the name extension of getInfo is not missing value then

if the length of the name extension of getInfo is greater than 3 then

my reportBadFile()

end if

end if

set nameHasDot to 0

repeat with i from 1 to (length of dropName)

if ((character i of dropName) is ".") then set nameHasDot to (nameHasDot + 1)

end repeat

if nameHasDot is greater than 1 then

my reportBadFile()

end if

set fileExt to ""

set nameOnly to ""

if nameHasDot is 1 then

-- set fileExt to ""

-- set nameOnly to ""

set posOfDot to (length of dropName) - (offset of "." in (reverse of characters of dropName as string)) + 1

if posOfDot is less than length of dropName then

set fileExt to text (posOfDot + 1) thru end of dropName

if posOfDot = 1 then

set nameOnly to ""

else

set nameOnly to text 1 thru (posOfDot - 1) of dropName

end if

end if

else

set nameOnly to dropName

-- set infileName to dropName

end if

if the length of nameOnly is greater than 8 then

my reportBadFile()

end if

if the length of fileExt is greater than 3 then

my reportBadFile()

end if

my DOSCharsOnly(nameOnly)

if nameOnly is not equal to fixedName then

my reportBadFile()

end if

set isWPDoc to false

set isWPDoc to ((read file (dropItem as text) from 1 to 4 as data) is «data rdatFF575043»)

if isWPDoc is false then

tell me to activate

display dialog "The file “" & dropName & "” does not seem to have been created by WordPerfect for DOS or Windows (version 5.x or later) or by WordPerfect for the Macintosh (version 2.x or 3.0 through 3.5.4)" & return & return & "If you select OK, I will try to open this file in WPDOS anyway." buttons {"OK", "Cancel"} default button 1 with title msgTitle

set userChoice to button returned of result

if userChoice is "Cancel" then

error number -128

else

set parentDirOnly to "no"

end if

end if

tell application "Finder"

-- set macWP to false

if the file type of dropItem is in the macTypeList then

-- set macWP to true

if wpVer is 51 then

tell me to activate

display dialog "WordPerfect 5.1 cannot open files created by WordPerfect for the Macintosh." buttons {"OK"} default button 1 with title msgTitle giving up after 15

error number -128

end if

end if

end tell

set setDot to "."

if fileExt is "" then

set setDot to ""

end if

set completeName to nameOnly & setDot & fileExt

set infileName to toUpper(completeName)

return infileName

end getInfileName


on DOSCharsOnly(testString)

if length of testString = 0 then return ""

set ATID to AppleScript's text item delimiters

set AppleScript's text item delimiters to {" ", "|", ".", "\"", "*", "/", "\\", "<", ">", ":", "="}

set wordlist to text items of testString

set AppleScript's text item delimiters to {""}

set fixedName to text items of wordlist as string

set AppleScript's text item delimiters to ATID

return fixedName

end DOSCharsOnly


on toUpper(theString)

return (do shell script "echo " & quoted form of theString & " | tr \"[:lower:]\" \"[:upper:]\"")

end toUpper


on reportBadFile()

tell me to activate

display dialog "I cannot open the file “" & dropName & "” automatically in WordPerfect because it does not have a standard DOS fileName." & return & return & "You may be able to open it from inside DOSBox." buttons {"OK"} default button 1 with title msgTitle giving up after 5

error number -128

end reportBadFile


on readPList() -- put in variable here

set myPlistFile to ((path to preferences as Unicode text) & myPlist & ".plist")

set runOnceFlag to ""

tell application "System Events"

try

set runOnceFlag to value of property list item "RunOnce" of property list file myPlistFile

on error

do shell script "defaults write " & myPlist & space & "RunOnce" & space & "true"

set firstRun to true

my showHelpFile()

end try

if runOnceFlag is "false" then

do shell script "defaults write " & myPlist & space & "RunOnce" & space & "true"

set firstRun to true

my showHelpFile()

end if

try

set use25Line to value of property list item "Use25Line" of property list file myPlistFile

on error

do shell script "defaults write " & myPlist & space & "Use25Line" & space & "false"

end try

try

set noFilePrompt to value of property list item "NoFilePrompt" of property list file myPlistFile

on error

do shell script "defaults write " & myPlist & space & "NoFilePrompt" & space & "false"

end try

try

set fullScreen to value of property list item "FullScreen" of property list file myPlistFile

end try

try

set useCustomBuild to value of property list item "UseCustomBuild" of property list file myPlistFile

end try

try

set fixFnKeys to value of property list item "FixFnKeys" of property list file myPlistFile

end try

try

set doMacroWarning to value of property list item "DoMacroWarning" of property list file myPlistFile

end try

end tell

end readPList


on showHelpFile()

set thisApp to (path to me) as text

set readmeFile to (thisApp & "Contents:Resources:Files:ReadMeFirst.pdf") as text

set readmePosix to quoted form of (POSIX path of (readmeFile as alias))

tell me to activate

set showHelp to display dialog "This seems to be the first time you have run this application." & return & return & "This application launches WordPerfect for DOS inside the DOSBox emulator software." & return & return & "Before using the application, you must copy some WordPerfect program files to the DOSDisk folder, as described in the ReadMe file." & return & return & "You may access the ReadMe file at any time by adding “Help” to my name." buttons {"Exit", "Continue", "Open ReadMe File"} default button 3 with icon note with title msgTitle

if button returned of showHelp is "Open ReadMe File" then

tell application "Preview"

activate

open file readmeFile as alias

end tell

activate

else if button returned of showHelp is "Exit" then

do shell script "defaults write " & myPlist & space & "RunOnce" & space & "false"

error number -128

end if

end showHelpFile


on checkKeyboardState()

set thisApp to path to me as text

set filesFolder to (thisApp & "Contents:Resources:Files:") as text

set filesFolder to filesFolder as alias

set checkKeysPath to (thisApp & "Contents:Resources:Files:checkModifierKeys")

set checkKeysPosix to quoted form of (POSIX path of checkKeysPath)

set optionState to "0"

set optionState to do shell script checkKeysPosix & space & "option"

if optionState is "1" then

my setOptions()

end if

end checkKeyboardState


on setOptions()

set screenOptionName to "Start DOSBox full-screen or windowed"

set set25LineModeName to "Start DOSBox in 43-line or 25-line mode"

set readmeOptionName to "Copy “Read Me First.pdf” file to OS X desktop"

set optionsList to {screenOptionName, set25LineModeName, readmeOptionName}

tell me to activate

set doOption to choose from list optionsList with prompt "Which option do you want to set?" OK button name "Set option" cancel button name "Finished setting options"

if doOption contains screenOptionName then

my screenSizeMenu()

else if doOption contains set25LineModeName then

my setLineCount()

else if doOption contains readmeOptionName then

set readmeFromWhere to "From setOptions"

my copyReadMeToDesktop(readmeFromWhere)

end if

end setOptions


on screenSizeMenu()

if fullScreen is "true" then

set screenSizeCurrent to " full-screen."

set screenButtonChoice to 1

else

set screenSizeCurrent to " windowed."

set screenButtonChoice to 2

end if

tell me to activate

display dialog "Do you want DOSBox to open in full-screen mode or in a window?" & return & return & "This option is curently set to" & screenSizeCurrent & return & return & "Whichever you choose, you can toggle between full-screen and windowed mode by pressing Cmd-Enter." buttons {"Full-screen", "Window"} default button screenButtonChoice with title msgTitle

if button returned of result is "Full-screen" then

set fullScreen to "true"

do shell script "defaults write " & myPlist & space & "FullScreen" & space & "true"

else

set fullScreen to "false"

do shell script "defaults write " & myPlist & space & "FullScreen" & space & "false"

end if

my setOptions()

end screenSizeMenu


on setLineCount()

if use25Line is "true" then

set lineCountCurrent to "25"

set lineButtonChoice to 2

else

set lineCountCurrent to "43"

set lineButtonChoice to 1

end if

tell me to activate

display dialog "Do you want DOSBox to open in 43-line or 25-line mode?" & return & return & "This option is currently set to" & space & lineCountCurrent & "." buttons {"43-line", "25-line"} default button lineButtonChoice with title msgTitle

if button returned of result is "25-line" then

set use25Line to "true"

do shell script "defaults write " & myPlist & space & "Use25Line" & space & "true"

else

set use25Line to "false"

do shell script "defaults write " & myPlist & space & "Use25Line" & space & "false"

end if

my setOptions()

end setLineCount


on copyReadMeToDesktop(readmeFromWhere)

set thisApp to path to me as text

set readmePDFFile to (thisApp & "Contents:Resources:Files:ReadMeFirst.pdf")

set readmePDFPosix to quoted form of (POSIX path of readmePDFFile)

set deskPosix to POSIX path of (path to desktop folder from user domain)

set pdfTargetPosix to quoted form of (deskPosix & "WPDOSBox ReadMeFirst.pdf")

set pdfReadMeExists to true

try

do shell script "ls -a " & pdfTargetPosix

on error

set pdfReadMeExists to false

end try

if pdfReadMeExists is true then

tell me to activate

display dialog "The WPDOSBox “ReadMeFirst.pdf” file already exists on your desktop. Do you want me to overwrite it?" buttons {"Yes", "No"} default button 1 with title msgTitle

if button returned of result is "Yes" then

try

do shell script "cp " & readmePDFPosix & space & pdfTargetPosix

on error e

tell me to activate

display dialog e

end try

tell me to activate

display dialog "The “Read Me First.pdf” file has been copied to your desktop as “WPDOSBox Read Me First.pdf.”" with title msgTitle buttons {"OK"} giving up after 5

else

tell me to activate

display dialog "The file has not been copied." buttons {"OK"} with title msgTitle giving up after 5

end if

else

try

do shell script "cp " & readmePDFPosix & space & pdfTargetPosix

on error e

tell me to activate

display dialog e

end try

tell me to activate

display dialog "The “Read Me First.pdf” file has been copied to your desktop as “WPDOSBox Read Me First.pdf.”" with title msgTitle buttons {"OK"} giving up after 5

end if

if readmeFromWhere is "From setOptions" then

my setOptions()

end if

end copyReadMeToDesktop