-- Self-contained WPMacApp SheepShaver setup  

-- Version 10.0.3 - 10 October 2013

-- Script by Edward Mendelson http://wpdos.org

-- Based on ideas by John Rethorst, applet includes work by Smokey Ardisson


-- all properties listed here should now be changed via setOptions

-- hold down Option key while launching


-- to do: setup universal-access equivalent for Mavericks

-- to do: document the transferImage feature


property parent : AppleScript


-- DEFAULT SCREEN SIZE 1024 x 768

property defaultWidth : 1024

property defaultHeight : 768


-- to specify window size permanently, use the SheepShaver Preferences dialog;

-- to make a portrait-layout window that WP can fill, use width 640, height 720 (or up to 742)


-- ENABLE DIRECT PRINTING TO LOCAL PRINTER (OS X 10.6 or 10.7 ONLY!!)

-- use this option to make it possible to use a local printer

--   directly, instead of using the special print system.

-- To disable direct printing to a local printer, set to 0

property enableLocalPtr : 1


-- the following lines are for debugging only; do not change them!

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

-- DO NOT PROMPT FOR PRINTERS

-- to suppress the prompt asking to setup a printer, change to 1

property suppressPrinterPrompt : 0


property firstRun : false

property showPrinterMenu : false

property doIPSetup : false

-- never change anything below these lines!

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

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


property msgTitle : "WPMac Appliance Launcher"

property fldrScript : "WPTransferFolderScript.scpt"

property reqVers : 6 -- requires Snow Leopard

property useHomeFolder : 0 -- if 1, Unix disk is user home folder, not documents folder

property calculateSize : 0

-- the default WPMacApp appliance size is 1024 x 768; however,

--   if you want the WPMacApp appliance screen size to be calculated

--   as a proportion of your Mac's screen size, change 0 to 1 or 2 below

-- for a calculated landscape-style layout, change 0 to 1

-- for a calculated portrait-style layout, change 0 to 2


global forceFullScreen, defaultScreenSize

global sheepApp, sheepPath, filesFolder, macSystem, macPath

global userPath, unixPosixPath, userLibPath, userPosixPath, unixPath

global vertBound, horizBound, maxHeight, maxWidth, fullScreen

global UNIX_plist_path, presMode, ssRunning, questionIcon

global thisApp, checkKeysPosix, myUnix, vmPath, vmPosix

global extfsPath, useSelectedUnix, readmeFile, readmePOSIX

global myPlist, myPlistfile


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

-- variables for installing printing system:

global userLibScrPath, thisApp, otherUnix -- userfldrpath

global printAction, neverFile, doPrompt, doSetup, sysAbsent

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

-- variables for Spotlight and QuickLook plugins and UA

global sysVers, Mountain, Mavericks -- Leopard, Lion

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

-- variables for dropped items

global unixFolder, posixFolder, actionChoice, runCount

global fileOnlyName, thePosPath, nameCount, aliasForList

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

-- variables for printer and PDFwriter installation

global pdfPkg, pdfPkgPath, doPrinterSetup, doPdfSetup

global thePrinter, theQueue, ptrButton

global printPDFprompt

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

-- variables for start WPMac automatically option

global wpStartup

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

-- variables for installing launch daemon

global plistHeadPosix, plistMiddlePosix, plistFootPosix, filesPosix, sswp


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

-- run and open

on run

doBasics()

runVM()

removePrintSystem()

error number -128

end run


on open (filesToOpen)

doBasics()

transferData(filesToOpen)

runVM()

removePrintSystem()

error number -128

end open


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

-- shared routines


on doBasics()

-- USE QUASI-FULLSCREEN MODE

-- to make SheepShaver Wrapper run in a quasi-fullscreen mode, use the SheepShaver

--  preferences dialog and set mode to fullscreen, not window

--   or, in preferences, set both height and width to 0

--   alternatively, to force fullscreen mode, change 0 to 1 below

-- Warning: this option hides the dock and menu bar when this app runs

-- NOTE THAT SETTING "forceFullScreen : 1" OVERRIDES "calculateSize : 1 or 2"

set forceFullScreen to 0

-- RESTORE DEFAULT SCREEN SIZE 1024x768

-- to restore default screen size change 0 to 1

set defaultScreenSize to 0

set doPrinterSetup to 0

set doPdfSetup to 0

set thePrinter to ""

set theQueue to ""

set ptrButton to ""

checkHostSystem()

readPlist() -- leads to showFirstRunMsg()

checkKeyboard() --leads to  setOptions()

screenDimension()

getMyName()

setupPaths()

writePrefs()

addPlugins()

setDataFolder()

writeOptionFiles()

if sswp is false then

setupLaunchDaemon()

else

setupSSWPLaunchDaemon()

end if

allPrinterSetup()

end doBasics


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

-- starting subroutines


(* -- this is NOT used yet

on processRunning(processName)

return (do shell script "ps axc") contains processName

end processRunning 

*)


on checkHostSystem()

set Mountain to 0

set Mavericks to 0

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

if sysVers < reqVers then

set dialogText to "This application runs under OS X 10." & reqVers & " or later, and you seem to have an earlier version of OS X."

errorQuit(dialogText)

end if

if sysVers is greater than or equal to 8 then set Mountain to 1

if sysVers is greater than or equal to 9 then set Mavericks to 1

if Mountain is 1 then

set suppressPrinterPrompt to 1

set enableLocalPtr to 0

end if

-- is another SheepShaver running?

tell application "System Events"

set apps to every process whose name contains "SheepShaver"

if not (apps is {}) then

set thisProcess to every process whose bundle identifier contains "org.wpdos.wpmacapp.sheepshaver"

if thisProcess is {} then

tell me to activate

display dialog "Another copy of SheepShaver seems to be running. I can continue, but you should not continue if the other copy uses the same disk image used by the WPMacApp." buttons {"Continue", "Cancel"} default button 1 with title msgTitle

if button returned of result is "Cancel" then

error number -128

end if

end if

end if

end tell

-- is this copy of SheepShaver already running?

set ssRunning to false

if application "WPMacApp" is running then

tell application "System Events"

set apps to every process whose bundle identifier is "org.wpdos.wpmacapp.sheepshaver"

if not (apps is {}) then

set ssRunning to true

end if

end tell

end if

end checkHostSystem


on checkKeyboard()

set showPrinterMenu to false

set doIPSetup to false

set thisApp to path to me as text

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

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

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

if optionState is "1" then

my setOptions()

end if

end checkKeyboard


on screenDimension()

if ssRunning is false then

set horizBound to 0

set vertBound to 0

-- routines to set screen size

set dspsizes to getDspSizes()

set widthList to {}

set heightList to {}

repeat with thisItem in dspsizes

set the end of widthList to first item in thisItem as number

set the end of heightList to second item in thisItem as number

end repeat

set sortedWidths to simple_sort(widthList)

set sortedHeights to simple_sort(heightList)

set maxWidth to the first item in sortedWidths as number

set maxHeight to the first item in sortedHeights as number

if calculateSize = 1 then

set horizBound to round maxHeight * 1.12

if horizBound is greater than 1600 then

set horizBound to 1600

else if horizBound is greater than 1280 then

set horizBound to 1280

else if horizBound is greater than 1152 then

set horizBound to 1152

end if

set vertBound to round horizBound * 0.75

set vertBound to vertBound as string

set horizBound to horizBound as string

else if calculateSize = 2 then

set horizBound to round maxHeight * 0.8

if horizBound is greater than 1600 then

set horizBound to 1600

else if horizBound is greater than 1280 then

set horizBound to 1280

else if horizBound is greater than 1152 then

set horizBound to 1152

end if

set vertBound to round horizBound * 1.125

set vertBound to vertBound as string

set horizBound to horizBound as string

end if

end if

end screenDimension


on setupPaths()

set useSelectedUnix to 1

try

do shell script "ls " & quoted form of extfsPath

on error

set useSelectedUnix to 0

end try

-- use Home or Documents folder?

if useHomeFolder = 1 then

set otherUnix to path to documents folder as text

else

set otherUnix to path to home folder as text

end if

if useSelectedUnix = 1 then

my setUnixFolder()

else

if useHomeFolder = 1 then

set unixPosixPath to POSIX path of (path to home folder)

set unixPath to path to home folder as text

-- set otherUnix to path to documents folder as text

else

set unixPosixPath to POSIX path of (path to documents folder)

set unixPath to path to documents folder as text

-- set otherUnix to path to home folder as text

end if

end if

-- write data path to plist file

set myPlist to "org.wpdos.wpmacapp"

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

try

do shell script "defaults write" & space & myPlist & space & "DataPath " & unixPosixPath

end try

set userPath to path to home folder as text

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

-- Set up paths in this app

if Mavericks is 0 then

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

else

set sheepApp to (path to me as string) & "Contents:Resources:Files:SheepShaver-MV.app" as string

end if

set sheepApp to sheepApp as alias

set sheepPath to (POSIX path of sheepApp)

try

set macSystem to (path to me as string) & "Contents:Resources:Files:MacOS.sheepvm" as string

set macSystem to macSystem as alias

set macPath to (POSIX path of macSystem)

on error

tell me to activate

set dialogText to "Add a self-contained sheepvm named MacOS.sheepvm to this application, in the folder Contents:Resources:Files."

errorQuit(dialogText)

end try

set sswp to true

set prefsPosix to (macPath & "prefs")

try

do shell script "head -n 1 " & quoted form of prefsPosix & " | grep SS-WP"

on error

set sswp to false

end try

set questionIcon to ((path to me as text) & "Contents:Resources:Files:Question.icns") as alias

set pdfPkg to (path to me as string) & "Contents:Resources:Files:PDFwriter.pkg" as string

set pdfPkg to pdfPkg as alias

set pdfPkgPath to quoted form of (POSIX path of pdfPkg)

set readmeFile to ((path to me as text) & "Contents:Resources:Files:Read Me First.pdf") as alias

set readmePOSIX to quoted form of POSIX path of readmeFile

set plistHeadPath to (thisApp & "Contents:Resources:Files:plisthead.txt")

set plistMiddlePath to (thisApp & "Contents:Resources:Files:plistmiddle.txt")

set plistFootPath to (thisApp & "Contents:Resources:Files:plistfoot.txt")

set plistHeadPosix to quoted form of (POSIX path of plistHeadPath)

set plistMiddlePosix to quoted form of (POSIX path of plistMiddlePath)

set plistFootPosix to quoted form of (POSIX path of plistFootPath)

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

set filesFolder to contentsFldr as alias

set filesPosix to (POSIX path of filesFolder)

end setupPaths


on setUnixFolder()

set vmPath to (path to me as string) & "Contents:Resources:Files:MacOS.sheepvm"

set vmFile to vmPath as alias

set vmPosix to (POSIX path of vmFile) as string

-- set extfsPosix to (vmPosix & "extfsposix")

set extfsPath to (vmPosix & "extfspath")

set unixPath to paragraph 1 of (read POSIX file extfsPath)

set unixPosixPath to POSIX path of file unixPath

if useHomeFolder = 1 then

set otherUnix to path to home folder as text

else

set otherUnix to path to documents folder as text

end if

end setUnixFolder


on writePrefs()

if ssRunning is false then

set prefsFile to (path to me) & "Contents:Resources:Files:MacOS.sheepvm:prefs" as string

set prefsFile to prefsFile as alias

set prefsPath to (POSIX path of prefsFile) as string

set savePath to ((POSIX path of prefsFile) & ".storage")

set tempPath to ((POSIX path of prefsFile) & ".tempfile")

set screenPath to ((POSIX path of prefsFile) & ".screen")

-- write prefs file, but first backup prefs

try

do shell script "cp " & quoted form of prefsPath & " " & quoted form of savePath

on error

set dialogText to "Problem backing up preferences file. No changes made."

end try

--  remove existing data before replacing

try

if (calculateSize is greater than 0) or (defaultScreenSize is greater than 0) then

do shell script "grep -v '^extfs\\|^screen' " & quoted form of prefsPath & " > " & quoted form of tempPath

else

do shell script "grep -v '^extfs' " & quoted form of prefsPath & " > " & quoted form of tempPath

end if

on error

set dialogText to "Problem extracting data from preference file. No changes made."

errorQuit(dialogText)

end try

--  insert replaced data

try

do shell script "echo extfs " & quoted form of unixPosixPath & ">> " & quoted form of tempPath

if defaultScreenSize is greater than 0 then

copy defaultWidth to horizBound

copy defaultHeight to vertBound

end if

if (calculateSize is greater than 0) or (defaultScreenSize is greater than 0) then

do shell script "echo screen win/" & horizBound & "/" & vertBound & " >> " & quoted form of tempPath

end if

on error

set dialogText to "Problem reading data from preference file. No changes made."

errorQuit(dialogText)

end try

try

do shell script "mv " & quoted form of tempPath & " " & quoted form of prefsPath

on error

set dialogText to "Problem creating new preferences file. No changes made."

errorQuit(dialogText)

end try

try

do shell script "rm " & quoted form of savePath

end try

-- routine for full-screen option

set fullScreen to 0

if forceFullScreen = 1 then

set fullScreen to 1

end if

set fullWidth to maxWidth as string

set fullHeight to maxHeight - 21 as string

set fullNumbers to fullWidth & "/" & fullHeight

-- did user specify "fullscreen" or 0/0 dimensions in SheepShaver prefs?

if fullScreen = 0 then

try

set fullByPrefs to true

set fullScreen to do shell script "grep -c 'dga/\\|/0/0' " & quoted form of prefsPath

on error

set fullByPrefs to false

end try

--

try

set fullByPrevious to true

set fullScreen to do shell script "grep -c '" & fullNumbers & "' " & quoted form of prefsPath

on error

set fullByPrevious to false

end try

if fullByPrefs is true then set fullScreen to 1

if fullByPrevious is true then set fullScreen to 1

end if

if fullScreen is greater than 0 then

-- set SheepShaver size to width of Mac by height minus menu line

-- set fullWidth to maxWidth as string

-- set fullHeight to maxHeight - 21 as string

try

do shell script "cp " & quoted form of prefsPath & " " & quoted form of savePath

on error

set dialogText to "Problem backing up preferences file. No changes made."

end try

try

do shell script "grep -v '^screen' " & quoted form of prefsPath & " > " & quoted form of tempPath

on error

set dialogText to "Problem reading screen size from preference file. No changes made."

errorQuit(dialogText)

end try

try

do shell script "echo screen win/" & fullWidth & "/" & fullHeight & " >> " & quoted form of tempPath

end try

try

do shell script "mv " & quoted form of tempPath & " " & quoted form of prefsPath

on error

set dialogText to "Problem writing new preferences file. No changes made."

errorQuit(dialogText)

end try

try

do shell script "rm " & quoted form of savePath

end try

-- modify info.plist so dock disapepars

set presMode to "LSUIPresentationMode"

set Mac_plist_path to (sheepPath as string) & "Contents/"

tell application "Finder"

set plistFile to (Mac_plist_path & "info.plist")

if not (exists (file (Mac_plist_path & "original.plist"))) then

try

do shell script "cp " & quoted form of POSIX path of plistFile & space & quoted form of POSIX path of (Mac_plist_path & "original.plist")

on error

tell me to activate

display dialog "Problem here."

end try

end if

end tell

set UNIX_plist_path to quoted form of POSIX path of (Mac_plist_path & "info")

set script_text to "defaults write " & UNIX_plist_path & space & presMode & space & "-int" & space & "4"

try

do shell script script_text

on error

set script_text to "defaults write " & UNIX_plist_path & space & presMode & space & "-int" & space & "1"

try

do shell script script_text

on error

tell me to activate

display dialog "Error writing SheepShaver defaults for WPMacApp." buttons {"OK"} default button 1 with icon stop giving up after 5

end try

end try

end if

end if

end writePrefs


on runVM()

-- now to run the program

if ssRunning is true then

do shell script "open -a" & space & quoted form of sheepPath

else

do shell script "open -a" & space & quoted form of sheepPath & space & quoted form of macPath

-- if full screen, nudge window to top of screen

if fullScreen is greater than 0 then

tell application "System Events"

repeat until (process "SheepShaver" exists)

delay 0.1

end repeat

tell process "SheepShaver"

repeat until window 1 exists

delay 0.1

end repeat

end tell

delay 0.7

tell process "SheepShaver"

tell window 1

set position to {0, 0}

end tell

end tell

end tell

delay 0.5

-- restore original info.plist settings

set script_text to "defaults write " & UNIX_plist_path & space & presMode & space & "-int" & space & "1"

try

do shell script script_text

on error

tell me to activate

display dialog "Error writing SheepShaver defaults for WPMacApp." buttons {"OK"} default button 1 with icon stop giving up after 5

end try

end if

end if

end runVM


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

-- subroutines 


on setDataFolder()

-- this creates .WPTransfer folder for communicating between OS X and SheepShaver

set unixFolder to ""

set text item delimiters to "/"

set posixItems to text items 2 thru -1 of unixPosixPath

set text item delimiters to ":"

set partialPath to (posixItems as string)

set unixFolder to (path to startup disk as string) & text 1 thru -1 of partialPath

-- tell me to activate

-- display dialog "debug " & sswp

if sswp is true then

set posixFolder to unixPosixPath & "SheepShaver Data/OSX > SS"

else

set posixFolder to unixPosixPath & ".WPTransfer"

end if

try

do shell script "ls -a " & quoted form of (posixFolder)

set folderFound to true

on error

set folderFound to false

end try

if folderFound is false then

do shell script "mkdir -p " & quoted form of posixFolder

end if

-- delete stray printer queue information file

try

do shell script "rm " & quoted form of (posixFolder & "/QueueName.txt")

end try

-- test for setupPDF alias file

set makeAlias to 0

if doPrinterSetup is 0 then

try

do shell script "ls -a " & quoted form of (posixFolder & "/MakePdfAlias")

set makeAlias to 1

on error

set makeAlias to 0

end try

if makeAlias is 1 then

try

do shell script "rm " & quoted form of (posixFolder & "/MakePdfAlias")

end try

my makePdfShortcut()

end if

end if

end setDataFolder


on writeOptionFiles()

if wpStartup is "true" then

try

do shell script "rm" & space & quoted form of (posixFolder & "/NoStartWP")

end try

else if wpStartup is "false" then

try

do shell script "echo ContentOfNoStartWPFile > " & quoted form of (posixFolder & "/NoStartWP")

on error errMsg

tell me to activate

display dialog errMsg

end try

end if

end writeOptionFiles


-- undocumented feature that adds disk image to self-contained sheepvm

on transferData(filesToOpen)

set fileCount to count of filesToOpen

----

set unixFolderCharCount to (count characters of unixFolder)

set fileList to ""

repeat with i in filesToOpen

set i to i as string

if (i contains ".dsk") or (i contains ".dmg") then

my transferImage(i)

end if

if not (i contains unixFolder) then

tell application "Finder" to set thisPosixName to POSIX path of (i as alias)

tell me to activate

display dialog "The file “" & thisPosixName & "” is not in the folder “" & unixPosixPath & "”, which corresponds to the “Unix” folder in the WPMacApp." & return & return & "The WPMacApp cannot access the file in its current location." & return & return & "Shall I open the folder “" & unixPosixPath & "” in the OS X Finder so that you can move the file into it?" buttons {"Yes, open the folder", "No, cancel"} default button 1 with icon caution with title msgTitle

if button returned of result is "Yes, open the folder" then

(*

do shell script "open " & quoted form of unixPosixPath

tell me to activate

display dialog "Please move the file “" & thisPosixName & "” to the folder “" & unixPosixPath & "”." & return & return & "After moving the file, double-click it to open it in the WPMacApp." buttons {"OK"} with title msgTitle with icon alias ":System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:OpenFolderIcon.icns" giving up after 10

*)

tell application "Finder"

activate

if front Finder window exists then

set this_folder to (the target of the front window) as alias

set {x1, y1} to position of front window

make new Finder window to this_folder

set position of front window to {(x1 + 50), (y1 + 150)} --This offsets the new window more than the average Finder tiling does

set target of front Finder window to (POSIX file unixPosixPath as text)

else

open (POSIX file unixPosixPath as text)

end if

display dialog "Please move the file “" & thisPosixName & "” to the folder “" & unixPosixPath & "”." & return & return & "After moving the file, double-click it to open it in the WPMacApp." buttons {"OK"} with title msgTitle with icon alias ":System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:OpenFolderIcon.icns" giving up after 10

end tell

end if

error number -128

else

set thePosPath to POSIX path of i

set fileOnlyName to do shell script "basename " & quoted form of thePosPath

set nameCount to (count characters of fileOnlyName)

if nameCount is less than 32 then

else

if ssRunning is false then

if fileCount is greater than 1 then

set dlgButtons to {"OK"}

else

set dlgButtons to {"Open the WPMacApp", "Cancel"}

end if

else

set dlgButtons to {"OK"}

end if

-- this tests for filename too long for OS 9

set runCount to 0

set aliasForList to ""

bigName()

copy aliasForList to i

set i to i as string

end if

-----

set iCount to (count characters of i)

if iCount is greater than 0 then

set netPath to text (unixFolderCharCount + 1) thru iCount of i

if netPath starts with ":" then

else

set netPath to ":" & netPath

end if

set unixPath to "Unix" & netPath -- removed ":" after Unix

set fileList to fileList & unixPath & return

end if

end if

end repeat

do shell script "echo " & quoted form of fileList & " >> " & quoted form of (posixFolder & "/wpFileList")

quit

end transferData


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

-- subroutines for dealing with old printing system


on removePrintSystem()

-- original use: if Unix folder has changed between home and documents, 

--remove printing system from other folder; 

-- Now revised to remove folder action script to avoid conflicts

tell application "Finder"

set ppExists to no

if exists folder (otherUnix & ".WPTransfer") then

set ppExists to yes

try

set otherPrintPath to alias (otherUnix & ".WPTransfer") as string

on error

tell me to activate

display dialog "Can't find old print path."

end try

end if

try

if exists folder (scriptPath & "Folder Action Scripts") then

set userScriptPath to alias (scriptPath & "Folder Action Scripts") as string

end if

end try

end tell

set actionScript to "WPTransferFolderScript.scpt"

tell application "System Events"

try

set curScripts to (attached scripts of otherPrintPath) as string

if curScripts contains actionScript then

if ppExists = yes then

try

tell folder action ".WPTransfer"

set enabled to false

set enabled of script "WPTransferFolderScript.scpt" to false

end tell

end try

try

-- Detaches the Folder Action from the folder

remove action from otherPrintPath using action name actionScript

end try

end if

end if

end try

end tell

tell application "Finder"

if exists file actionScript then

delete file actionScript

end if

(*

-- if PrintFiles folder is empty, then delete it !!!

if ppExists = yes then

set ppCount to count of (files in folder otherPrintPath)

if ppCount = 0 then

delete otherPrintPath

else

set dialogText to "The hidden “.WPTransfer” folder in your user folder contains files that you may want to save." & return & return & "The folder will now open. Move any files you wish to keep, then delete this folder."

tell me to activate

display dialog dialogText buttons {"OK"} default button 1 with icon caution

open otherPrintPath

end if

end if

*)

end tell

end removePrintSystem


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

-- 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 at 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


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

-- Other Subroutines


on getDspSizes()

set dspsizes to {}

set toolpath to ((path to me) as Unicode text) & "Contents:Resources:dspsizes"

set command to (quoted form of POSIX path of toolpath)

set output to paragraphs of (do shell script command)

repeat with dspinfo in output

set dspsizes to dspsizes & {({word 1, word 3} of dspinfo)}

end repeat

end getDspSizes


on simple_sort(my_list)

set the index_list to {}

set the sorted_list to {}

repeat (the number of items in my_list) times

set the low_item to ""

repeat with i from 1 to (number of items in my_list)

if i is not in the index_list then

set this_item to item i of my_list as text

if the low_item is "" then

set the low_item to this_item

set the low_item_index to i

else if this_item comes before the low_item then

set the low_item to this_item

set the low_item_index to i

end if

end if

end repeat

set the end of sorted_list to the low_item

set the end of the index_list to the low_item_index

end repeat

return the sorted_list

end simple_sort


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 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 bigName()

set runCount to runCount + 1

if runCount = 1 then set actionChoice to "copy"

set fullPathLength to (count characters of thePosPath)

set folderString to text 1 thru (fullPathLength - nameCount) of thePosPath

copy thePosPath to oldName

set newName to text 1 thru 31 of fileOnlyName

set changeToName to newName & "X" -- dummy variable to get into repeat loop

set addText to ""

set tries to 0

repeat

tell (count characters of changeToName) to if it > 0 and it < 32 then exit repeat

if tries > 0 and (count characters of changeToName) > 0 then

set addText to "Please enter a name with no more than 31 characters. The name you entered has " & (count characters of changeToName) & " characters." & return & return

else

set addText to ""

end if

set defBtn to 2

if actionChoice = "rename" then

set defBtn to 3

end if

tell me to activate

set fixName to display dialog "The file you selected cannot be opened in WPMacApp because its name has more than 31 characters: " & return & return & "“" & fileOnlyName & "”" & return & return & "If you choose, I can copy this file to a new file with a shorter name, or I can rename the original file. I will then try to open the copied or renamed file in WPMacApp." & return & return & "To copy or rename this file, accept or change the new name shown below. Do not enter more than 31 characters." & return & return & "Click “Skip this file” to continue without changing or opening this file." & return & return & "Click “Copy to new file” to create a copy of the file in the same folder as the original, with the new name that you enter below; the original file will not be changed." & return & return & "Click “Rename existing file” to rename the original file with the new name that you enter below." & return & return & addText & "Enter a new name below:" default answer newName buttons {"Skip this file", "Copy to new file", "Rename existing file"} default button defBtn with icon caution with title msgTitle

set changeToName to text returned of the result

set tries to tries + 1

if button returned of fixName = "Skip this file" then

set aliasForList to ""

return

end if

if button returned of fixName = "Copy to new file" then

set actionChoice to "copy"

end if

if button returned of fixName = "Rename existing file" then

set actionChoice to "rename"

end if

end repeat

set theQuestion to ""

if actionChoice = "copy" then

set theQuestion to "Proceed with copying your original file:" & return & return & "“" & fileOnlyName & "”" & return & return & "to a new file named:" & return & return & "“" & changeToName & "”"

end if

if actionChoice is "rename" then

set theQuestion to "Proceed with renaming your file:" & return & return & "“" & fileOnlyName & "”" & return & return & "with the new name:" & return & return & "“" & changeToName & "”"

end if

set icnPath to (path to "csrv")'s POSIX path & "CoreTypes.bundle/Contents/Resources/"

tell me to activate

set confirmChange to display dialog theQuestion buttons {"No, choose a different name", "Yes, continue"} default button 2 with icon questionIcon with title msgTitle

-- OR with icon POSIX file (icnPath & "MigrateAsst.icns") 

if button returned of confirmChange is "No, choose a different name" then

set changeToName to ""

bigName()

end if

if button returned of confirmChange is "Yes, continue" then

set outputFile to folderString & changeToName

set targetExists to true

try

do shell script "ls " & quoted form of outputFile

on error

set targetExists to false

end try

if targetExists is true then

tell me to activate

set overWrite to display dialog "Target file:" & return & return & "“" & changeToName & "”" & return & return & "already exists. Overwrite?" buttons {"No", "Yes"} default button 2 with icon caution with title msgTitle

if button returned of overWrite is "No" then

set changeToName to ""

set outputFile to ""

bigName()

end if

end if

if actionChoice = "rename" then

set shellVerb to "mv"

end if

if actionChoice = "copy" then

set shellVerb to "cp"

end if

do shell script shellVerb & space & quoted form of thePosPath & space & quoted form of outputFile

set aliasForList to POSIX file outputFile

end if

end bigName


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

-- subroutines for setting up local printers


on localPrinter()

-- turn on lpd for remote printing

try

set cupsctlString to do shell script "cupsctl | grep -i \"BrowseLocalProtocols\""

end try

if cupsctlString contains "lpd" then

else

if Mountain is 1 then

do shell script "cupsctl 'BrowseLocalProtocols=\"dnssd lpd\"'"

else

do shell script "cupsctl 'BrowseLocalProtocols=\"cups dnssd lpd\"'"

end if

end if

end localPrinter


on prepareIPPrinter()

tell me to activate

display dialog "This option is to be used only to set up WPMac to print to a networked PostScript printer." & return & return & "It will not work with a printer connected by a USB cable." & return & return & "Your printer must have an IP address on your ethernet or wireless network." buttons {"Continue setup", "Cancel"} default button 2 with title msgTitle

if button returned of result is "Cancel" then

display dialog "Quitting. Please launch me again." buttons {"OK"} with title msgTitle giving up after 20

end if

set noPrinters to 0

try

set printerNames to (do shell script "lpstat  -l -p | grep -i Description: |awk -F'Description: ' '{print $2}' ") -- as list

on error

set noPrinters to 1

end try

if printerNames = "" then

set noPrinters to 1

end if

if noPrinters is 0 then

try

set queueNames to (do shell script "lpstat -a | awk -F' accepting' '{print $1}'") -- as list

on error

set noPrinters to 1

end try

end if

if noPrinters = 0 then

try

set printerList to (every paragraph of printerNames) as list

set queueList to (every paragraph of queueNames) as list

on error

tell me to activate

display dialog "No OS X printers available to set up." & return & return & "When you have installed a networked PostScript printer and want to use it in WordPerfect, hold down the Option key when launching this application and select the Set Up Networked PostScript Printer option again." buttons {"OK", "Quit"} with title msgTitle

if button returned of result is "Quit" then

error number -128

end if

try

do shell script "echo " & theQueue & " > " & quoted form of (posixFolder & "/SetupDone")

end try

return

end try

else

tell me to activate

display dialog "No OS X printers available to set up." & return & return & "When you have installed a networked PostScript printer and want to use it in WordPerfect, hold down the Option key when launching this application and select the Set Up Networked PostScript Printer option again." buttons {"OK", "Quit"} with title msgTitle

if button returned of result is "Quit" then

error number -128

end if

try

do shell script "echo " & theQueue & " > " & quoted form of (posixFolder & "/SetupDone")

end try

return

end if

if printerList is "" then

tell me to activate

display dialog "No printers available to set up." & return & return & "When you have installed a networked PostScript printer and want to use it in WordPerfect, hold down the Option key when launching this application and select the Set Up Networked PostScript printer option again." buttons {"OK", "Quit"} with title msgTitle

if button returned of result is "Quit" then

error number -128

end if

try

do shell script "echo " & theQueue & " > " & quoted form of (posixFolder & "/SetupDone")

end try

return

end if

tell me to activate

set thePrinter to (choose from list printerList with title "Printers" with prompt "Choose a networked PostScript printer to use with WordPerfect:")

-- removed from previous parenthesis: default items {item 1 of printerList} 

if thePrinter is false then

tell me to activate

display dialog "Printer setup cancelled. If you press OK or wait a few seconds, I will continue to launch the WPMacApp." buttons {"OK", "Cancel"} default button 1 with title msgTitle giving up after 5

if button returned of the result is "Cancel" then

error number -128

end if

set doPrinterSetup to 0

return

else

set thePrinter to item 1 of thePrinter

set item_num to my list_position(thePrinter, printerList)

set theQueue to item item_num in queueList

end if

if ":" is in thePrinter then

display dialog "The printer name may not include a colon. Please rename the printer in OS X and try again." buttons {"OK"} with title msgTitle giving up after 20

error number -128

end if

set dnsURL to do shell script "lpstat -v " & theQueue

set dnsString to (do shell script "perl -e 'use URI::Escape; print uri_unescape(\"" & dnsURL & "\")';")

try

set thisApp to path to me as text

set bonjourEventsPath to (thisApp & "Contents:Resources:Files:Bonjour Events.app")

set bonjourEventsPosix to quoted form of (POSIX path of bonjourEventsPath)

set bonjourEventsAlias to bonjourEventsPath as alias

-- tell application "Bonjour Events"

tell application id "com.klieme.bonjourevents"

scan for type "_ipp._tcp" given «class data»:"local"

repeat until browser finished

delay 0.5

end repeat

if (count services) > 0 then

set nameList to name of services

set addressList to IPv4 address of services

else

set nameList to {}

set addressList to {}

end if

quit

-- Bonjour Events quits automatically after 2 minutes of inactivity

end tell

on error errorMessage number errorNumber

tell me to activate

display dialog "An error occurred: " & errorMessage & " (" & errorNumber & ")"

end try

set ipPrinterFound to false

repeat with currentItem in nameList

if currentItem is in dnsString then

set ipPrinterFound to true

set currentListItem to currentItem as item

set dnsNumber to my list_position(currentListItem, nameList)

set printerIP to item dnsNumber in addressList

-- tell me to activate

-- display dialog currentItem & return & return & "has the IP address:" & space & printerIP

exit repeat

end if

end repeat

if ipPrinterFound is false then

display dialog thePrinter & " does not seem to support IP printing." & return & return & "Quitting. Please launch me again." with title msgTitle buttons {"OK"} giving up after 20

error number -128

end if

do shell script "echo " & printerIP & " > " & quoted form of (posixFolder & "/IPAddress.unix")

do shell script "echo " & thePrinter & " >> " & quoted form of (posixFolder & "/IPAddress.unix")

do shell script "cd " & quoted form of posixFolder & " ; tr '\\n' '\\r' < IPAddress.unix > IPAddress.txt"

do shell script "rm " & quoted form of (posixFolder & "/IPAddress.unix")

-- debug end

end prepareIPPrinter


on setupPrinter()

if doPrinterSetup is 1 then

if enableLocalPtr is 1 then

my localPrinter()

end if

set noPrinters to 0

try

set printerNames to (do shell script "lpstat  -l -p | grep -i Description: |awk -F'Description: ' '{print $2}' ") -- as list

on error

set noPrinters to 1

end try

if printerNames = "" then

set noPrinters to 1

end if

if noPrinters is 0 then

try

set queueNames to (do shell script "lpstat -a | awk -F' accepting' '{print $1}'") -- as list

on error

set noPrinters to 1

end try

end if

if noPrinters = 0 then

try

set printerList to (every paragraph of printerNames) as list

set queueList to (every paragraph of queueNames) as list

on error

tell me to activate

display dialog "No OS X printers available to set up." & return & return & "When you have installed a printer and want to use it in WordPerfect, hold down the Option key when launching this application and select the printer options again." buttons {"OK", "Quit"} with title msgTitle

if button returned of result is "Quit" then

error number -128

end if

try

do shell script "echo " & theQueue & " > " & quoted form of (posixFolder & "/SetupDone")

end try

return

end try

else

tell me to activate

display dialog "No OS X printers available to set up." & return & return & "When you have installed a printer and want to use it in WordPerfect, hold down the Option key when launching this application and select the printer options again." buttons {"OK", "Quit"} with title msgTitle

if button returned of result is "Quit" then

error number -128

end if

try

do shell script "echo " & theQueue & " > " & quoted form of (posixFolder & "/SetupDone")

end try

return

end if

if printerList is "" then

tell me to activate

display dialog "No printers available to set up." & return & return & "When you have installed a printer and want to use it in WordPerfect, hold down the Option key when launching this application and select the printer options again." buttons {"OK", "Quit"} with title msgTitle

if button returned of result is "Quit" then

error number -128

end if

try

do shell script "echo " & theQueue & " > " & quoted form of (posixFolder & "/SetupDone")

end try

return

end if

if the (count of printerList) is 1 then

set thePrinter to {item 1 of printerList} as string

set theQueue to {item 1 of queueList} as string

if ":" is in thePrinter then

display dialog "The printer name may not include a colon. Please rename the printer in OS X and try again." buttons {"OK"} with title msgTitle giving up after 20

error number -128

end if

-- if theQueue is "PDFwriter" then set chosePdfwriter to 1

tell me to activate

display dialog "I will now set up " & thePrinter & " for use in WordPerfect." buttons {"OK"} with title msgTitle giving up after 5

else

tell me to activate

set thePrinter to (choose from list printerList with title "Printers" with prompt "Choose a printer to use with WordPerfect:")

-- removed from previous parenthesis: default items {item 1 of printerList} 

if thePrinter is false then

tell me to activate

display dialog "Printer setup cancelled. If you press OK or wait a few seconds, I will continue to launch the WPMacApp." buttons {"OK", "Cancel"} default button 1 with title msgTitle giving up after 5

if button returned of the result is "Cancel" then

error number -128

end if

set doPrinterSetup to 0

return

else

set thePrinter to item 1 of thePrinter

set item_num to my list_position(thePrinter, printerList)

set theQueue to item item_num in queueList

end if

if ":" is in thePrinter then

display dialog "The printer name may not include a colon. Please rename the printer in OS X and try again." buttons {"OK"} with title msgTitle giving up after 20

error number -128

end if

end if

do shell script "echo " & theQueue & " > " & quoted form of (posixFolder & "/QueueName.unix")

do shell script "echo " & thePrinter & " >> " & quoted form of (posixFolder & "/QueueName.unix")

do shell script "cd " & quoted form of posixFolder & " ; tr '\\n' '\\r' < QueueName.unix > QueueName.txt"

do shell script "rm " & quoted form of (posixFolder & "/QueueName.unix")

tell application "System Events"

if Mavericks is 0 then

if UI elements enabled then

else

my setupUniversalAccess()

end if

else

my setupMavericksAccess()

end if

end tell

-- to do: add fix for wrong pane opening in "System Preferences"

tell application "System Preferences"

activate

reveal anchor "Main" of pane id "com.apple.preferences.sharing"

end tell

tell application "System Events"

tell process "System Preferences"

tell table 1 of scroll area 1 of group 1 of window 1

tell (1st row whose value of static text 1 is "Printer Sharing")

set selected to true

if value of checkbox 1 is 0 then

click checkbox 1

end if

end tell

end tell

delay 0.5

(* -- leave this here in case of future changes

if Leopard is 1 then

set scrollArea to 2

else

set scrollArea to 3

end if

*)

tell table 1 of scroll area 3 of group 1 of window 1

tell (1st row whose value of text field 1 is thePrinter)

set selected to true

if value of checkbox 1 is 0 then

click checkbox 1

end if

end tell

end tell

end tell

end tell

tell me to activate

display dialog "Please confirm (1) that Printer Sharing is checked in the column on the left and (2) that " & thePrinter & " is checked in the list of printers in the Printer Sharing menu, then click OK." & return & return & "You must add the necessary checkmarks if they are not present." with title msgTitle

tell application "System Preferences" to quit

set doPrinterSetup to 0

end if

-- debug

end setupPrinter


on list_position(this_item, this_list)

repeat with i from 1 to the count of this_list

if item i of this_list is this_item then return i

end repeat

return 0

end list_position


on setupPdf()

if doPdfSetup is 1 then

if enableLocalPtr is 1 then

my localPrinter()

end if

-- test whether PDFwriter is installed

try

do shell script "ls -a /Library/Printers/Lisanet/PDFwriter/pdfwriter"

on error

-- install PDFwriter if it's not already installed

(*

if Lion is 1 then

display dialog "PDFwriter seems not to be installed. This is the OS X software required for use with the WPMacApp PDF writer." & return & return & "Under OS X 10.7 or later, I strongly recommend that you install PDFwriter separately." & return & return & "I can take you to the download page for the PDFwriter Installer." & return & return & "After installing PDFwriter, complete its installation by printing any document to it from OS X, and then launch this WPMac appplication again (holding down Option-Cmd when starting it) to complete the WordPerfect setup for PDFwriter." buttons {"Open download page", "Cancel"} with title msgTitle

if button returned of result is "Open download page" then

open location "http://sourceforge.net/projects/pdfwriterformac/"

end if

error number -128

end if

*)

tell me to activate

display dialog "Please follow the prompts to install PDFwriter. Accept the default settings." buttons {"OK", "Cancel"} default button 1 with title msgTitle giving up after 20

if button returned of the result is "Cancel" then

tell me to activate

display dialog "Script cancelled." buttons {"OK"} with title msgTitle giving up after 10 with icon stop

error number -128

end if

tell application "System Events"

set procList to (name of every process)

end tell

if procList contains "Installer" then

tell me to activate

display dialog "Please quit any installation program that may be running and try again." buttons {"OK"} giving up after 10

error number -128

end if

--tell application "Finder" to open pdfPkg

do shell script "open " & pdfPkgPath

set pdfRunning to true

repeat until pdfRunning is false

tell application "System Events"

set procList to (name of every process)

end tell

if procList contains "Installer" then

else

set pdfRunning to false

end if

delay 1

end repeat

end try

try

do shell script "lpadmin -p PDFwriter -v PDFwriter:/ -m PDFwriter.ppd -E -o printer-is-shared=true"

do shell script "lp -d PDFwriter -o landscape " & readmePOSIX

on error

tell me to activate

display dialog "Error creating PDFwriter printer. Exiting." with title msgTitle buttons {"OK"} giving up after 10

error number -128

end try

set pdfOK to 1

try

set pdfOK to (do shell script "lpstat -p | grep -c 'PDFWriter'")

on error errMsg

if errMsg is "0" then set pdfOK to 0

end try

if pdfOK is 0 then

tell me to activate

display dialog "PDFwriter installation seems not to have completed. Please restart this application and start again." buttons {"OK"} with title msgTitle giving up after 5

error number -128

end if

set thePrinter to "PDFwriter" as string

set theQueue to "PDFwriter" as string

do shell script "echo " & theQueue & " > " & quoted form of (posixFolder & "/QueueName.unix")

do shell script "echo " & thePrinter & " >> " & quoted form of (posixFolder & "/QueueName.unix")

do shell script "cd " & quoted form of posixFolder & " ; tr '\\n' '\\r' < QueueName.unix > QueueName.txt"

do shell script "rm " & quoted form of (posixFolder & "/QueueName.unix")

tell application "System Events"

if Mavericks is 0 then

if UI elements enabled then

else

my setupUniversalAccess()

end if

else

my setupMavericksAccess()

end if

end tell

tell application "System Preferences"

activate

reveal anchor "Main" of pane id "com.apple.preferences.sharing"

end tell

tell application "System Events"

tell process "System Preferences"

tell table 1 of scroll area 1 of group 1 of window 1

tell (1st row whose value of static text 1 is "Printer Sharing")

set selected to true

if value of checkbox 1 is 0 then

click checkbox 1

end if

end tell

end tell

delay 0.5

(*

if Leopard is 1 then

set scrollArea to 2

else

set scrollArea to 3

end if

*)

tell table 1 of scroll area 3 of group 1 of window 1

tell (1st row whose value of text field 1 is "PDFwriter")

set selected to true

if value of checkbox 1 is 0 then

click checkbox 1

end if

end tell

end tell

end tell

end tell

tell me to activate

display dialog "Please confirm that " & thePrinter & " is checked in the list of printers in the Printer Sharing menu, then click OK." & return & return & "Add a checkmark if it is not present."

tell application "System Preferences" to quit

end if

set doPdfSetup to 0

end setupPdf


on makePdfShortcut()

-- display dialog "If you do not already have an alias to the output directory for PDF files created by the PDFwriter driver in the WPMacApp, I will create one for you." & return & return & "Please wait while I search for an existing alias." buttons {"OK"} with title msgTitle giving up after 7

set searchLoc to "-onlyin ~/ "

set prefixCmd to "mdfind "

-- 'Alias' is in (escaped) double quotes

-- the command is in single quotes

-- the string is in double quotes

set suffixCmd to "'kMDItemKind == \"Alias\"'"

-- use Spotlight to find aliases

set myCommand to prefixCmd & searchLoc & suffixCmd

set theList to paragraphs of (do shell script myCommand)

set foundAlias to 0

tell application "Finder"

repeat with p in theList

set thisAlias to p as POSIX file as alias -- coerce Unicode text to POSIX file path to AppleScript alias

try

if original item of thisAlias is folder "anonymous users" of folder "PDFwriter" of folder "spool" of folder "var" of item "private" of startup disk then

set foundAlias to 1

set aliasPath to p as string

exit repeat

end if

end try

end repeat

end tell

if foundAlias is 1 then

else

tell application "Finder"

try

set pdfFolder to (POSIX file "/private/var/spool/PDFwriter/anonymous users") as alias

on error

tell me to activate

display dialog "PDF output folder not found. Please reinstall PDFwriter."

end try

set madeAlias to 0

try

make new alias file at (path to the desktop folder from user domain) to pdfFolder with properties {name:"WPMac-PDF"}

set madeAlias to 1

on error

tell me to activate

display dialog "Could not create an alias to the PDF output folder"

end try

end tell

if madeAlias is 1 then

tell me to activate

display dialog "I created an alias on your desktop named WPMac-PDF. This is an alias to the folder that contains the output of the PDFwriter printer in the WPMacApp. You may move this alias anywhere you choose." & return & return & "If you choose to delete the alias, you may find the PDF output at" & return & return & "/Users/Shared/PDFwriter/anonymous users" buttons {"OK"} with title msgTitle

end if

end if

end makePdfShortcut


on printerPrompt(ptrFromWhere)

if suppressPrinterPrompt is 1 then

if Mountain is 1 then

tell me to activate

display dialog "Under OS X 10.8 or later, the only reliable method of printing from the WPMacApp is through the default “Print or Create PDF from OS X” desktop printer." & return & return & "Please read the “Read Me First.pdf” file for detailed instructions on printing." buttons {"OK"} with title msgTitle giving up after 10

try

do shell script "echo ContentOfNeverAskFile > " & quoted form of (posixFolder & "/NeverAsk")

end try

end if

else

set doPdfSetup to 0

set ptrButton to ""

tell me to activate

display dialog "Set up an OS X printer or a PDF writer for use in the WPMacApp?" & return & return & "(To install the PDF writer or a printer at a later time, hold down the Option key when starting the application.)" buttons {"Setup printer", "Setup PDF writer", "Not now"} default button 1 with title msgTitle

set ptrButton to button returned of the result

if ptrButton is "Setup printer" then

set doPrinterSetup to 1

set doPdfSetup to 0

else

if ptrButton is "Setup PDF writer" then

set doPrinterSetup to 0

set doPdfSetup to 1

end if

end if

end if

if doPrinterSetup is 1 then

my setupPrinter()

else if doPdfSetup is 1 then

my setupPdf()

end if

end printerPrompt


on setupUniversalAccess()

tell application "System Events"

if UI elements enabled then

else

-- GUI scripting not enabled; display an alert

tell me to activate

display dialog "Before you can use this option, 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 application "System Preferences"

activate

set current pane to pane id "com.apple.preference.universalaccess"

end tell

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 & "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

end if

end if

end tell

delay 1

end setupUniversalAccess


on setupMavericksAccess()

-- debug

(*

tell me to activate

display dialog "This feature is not currently available under OS X 10.9 Mavericks." & return & return & "With luck, it will be restored in a later version of this application." & return & return & "I will now close." buttons {"OK"} default button 1 with icon caution with title msgTitle

quit

error number -128

*)

tell application "System Events"

if UI elements enabled then

else

-- GUI scripting not enabled; display an alert

tell me to activate

display dialog "Before you can use this option, you must enable “Accessibility” for this application." & return & return & "May I turn on this feature?" & return & return & "If you answer Yes, OS X will present a prompt and open System Preferences. Click on the padlock at the lower left, enter your password, and check the box next to the name WPMacApp." buttons {"Cancel", "Yes"} default button 2 with icon caution with title msgTitle

set enableGUI to button returned of result

if enableGUI is "Cancel" then

error number -128

else

tell application "System Preferences"

activate

reveal anchor 5 of pane id "com.apple.preference.security"

end tell

tell me to activate

display dialog "Enable accessibility for this application in System Preferences, then return to the options menu." buttons {"OK"} with title msgTitle giving up after 10

end if

end if

end tell

delay 1

-- end debug

end setupMavericksAccess


on getMyName()

-- in case we need to use selected unix folder

set vmPath to (path to me) & "Contents:Resources:Files:MacOS.sheepvm" as string

set vmFile to vmPath as alias

set vmPosix to (POSIX path of vmFile) as string

-- set extfsPosix to (vmPosix & "extfsposix")

set extfsPath to (vmPosix & "extfspath")

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 extraString to " "

if myName contains "setup" then

set extraString to return & return & "Remove “Setup” from my name to hide this menu next time."

set fromWhere to "From getMyName"

my askforUnix(extraString, fromWhere)

end if

end getMyName


on askforUnix(extraString, fromWhere)

tell me to activate

display dialog "Select an OS X folder to use as the “Unix” folder in SheepShaver." & extraString buttons {"Documents folder", "Home folder", "Choose another…"} default button 1 with title msgTitle

if button returned of result is "Documents folder" then

set useHomeFolder to 0

try

-- do shell script "rm " & quoted form of extfsPosix

do shell script "rm " & quoted form of extfsPath

end try

else if button returned of result is "Home folder" then

set useHomeFolder to 1

try

-- do shell script "rm " & quoted form of extfsPosix

do shell script "rm " & quoted form of extfsPath

end try

else if button returned of result is "Choose another…" then

try

set myUnix to choose folder with prompt "Select a folder for the “Unix” folder in SheepShaver and click Choose:"

set unixPath to myUnix as text

-- do shell script "echo " & quoted form of unixPosixPath & " > " & quoted form of extfsPosix

do shell script "echo " & unixPath & " > " & quoted form of extfsPath

on error

-- if fromWhere is "From setOptions" then

-- my setOptions()

-- end if

end try

(*

-- set unixPosixPath to POSIX path of myUnix

set unixPath to myUnix as text

-- do shell script "echo " & quoted form of unixPosixPath & " > " & quoted form of extfsPosix

do shell script "echo " & unixPath & " > " & quoted form of extfsPath

*)

end if

if fromWhere is "From setOptions" then

my setOptions()

end if

end askforUnix


on transferImage(i)

tell application "Finder"

set dskName to name of (i as alias)

end tell

tell me to activate

display dialog dskName & " seems to be a disk image file. What do you want me to do with this file?" & return & return & "I can set it to be the boot disk of the WPMacApp, instead of the existing boot disk." & return & return & "Or I can add it to the WPMacApp as a second disk, retaining the existing boot disk." buttons {"Set as boot disk", "Keep existing and add new", "Cancel"} default button 3 with title msgTitle

if button returned of result is "Cancel" then

error number -128

-- replace existing disk

else if button returned of result is "Set as boot disk" then

set moveBootDisk to 0

display dialog "Which of these two steps do you want me to take?" & return & return & "Do you want me to replace the existing WPMacApp boot disk image file with the “" & dskName & "” disk image (and move the “" & dskName & "” disk image into the WPMacApp application bundle)?" & return & return & "Or do you want me to leave the “" & dskName & "” disk image where it is and set it to be the WPMacApp boot disk?" buttons {"Move disk to app", "Leave it and set as boot disk", "Cancel"} default button 3 with title msgTitle

if button returned of result is "Cancel" then

error number -128

else if button returned of result is "Move disk to app" then

set moveBootDisk to 1

end if

tell application "Finder" to set thisPosixName to POSIX path of (i as alias)

set macSystem to (path to me as text) & "Contents:Resources:Files:MacOS.sheepvm" as string

set macSystem to macSystem as alias

set oldPath to ((POSIX path of macSystem) & "WPMac86HD.dsk")

if moveBootDisk is 1 then

tell me to activate

display dialog "I will now move your existing boot disk to the trash and replace it with your “" & dskName & "” disk image. The “" & dskName & "” disk image will be moved into the WPMacApp application bundle." & return & return & "If you want to set the old boot disk as the non-booting second disk, then, after I quit, remove the “WPMac86HD.dsk” file from the trash and drop it on me to set it as the second disk." buttons {"OK", "Cancel"} default button 1 with title msgTitle

if button returned of result is "Cancel" then

error number -128

end if

try

do shell script "test -e " & quoted form of oldPath

on error

display dialog "Old system boot disk not found." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end try

try

set oldPathAlias to (POSIX file oldPath) as alias

on error

display dialog "Can't create alias for old boot disk." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end try

tell application "Finder"

try

delete oldPathAlias

on error

tell me to activate

display dialog "Error moving old disk image file to the trash." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end try

end tell

tell me to activate

display dialog "Please wait a few seconds while I copy the file." & return & return & "I will reappear when the copy is complete." buttons {"OK"} with title msgTitle giving up after 2

try

do shell script "cp " & quoted form of thisPosixName & space & quoted form of oldPath

on error

tell me to activate

display dialog "Problem copying disk image file. Quitting." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end try

tell me to activate

display dialog "The SheepShaver disk image file has been replaced with a copy of “" & dskName & "” disk image. The old disk image has been moved to the trash." & return & return & "Please restart me." & return & return & "If I fail to boot completely, press Ctrl-Esc to force me to quit and start me again." buttons {"OK"} with title msgTitle giving up after 10

error number -128

else

set prefsFile to (path to me as string) & "Contents:Resources:Files:MacOS.sheepvm:prefs" as string

set prefsFile to prefsFile as alias

set prefsPath to (POSIX path of prefsFile) as string

set savePath to ((POSIX path of prefsFile) & ".storage")

set tempPath to ((POSIX path of prefsFile) & ".tempfile")

set diskPath to ((POSIX path of prefsFile) & ".diskfile")

try

do shell script "cp " & quoted form of prefsPath & " " & quoted form of savePath

on error

set dialogText to "Problem backing up preferences file. No changes made."

end try

do shell script "grep -v '^disk' " & quoted form of prefsPath & " > " & quoted form of tempPath

do shell script "echo disk " & thisPosixName & ">> " & quoted form of diskPath

do shell script "cat " & quoted form of diskPath & space & quoted form of tempPath & " > " & quoted form of prefsPath

do shell script "rm " & quoted form of diskPath

do shell script "rm " & quoted form of tempPath

do shell script "rm " & quoted form of savePath

set oldBootDiskFound to 1

try

do shell script "test -e " & quoted form of oldPath

on error

set oldBootDiskFound to 0

end try

if oldBootDiskFound is 1 then

display dialog "Shall I move the old boot disk in the WPMacApp to the trash?" & return & return & "If you click No, the old boot disk image file will remain inside the WPMacApp but will not be used for any purpose." & return & return & "If you want to set the old boot disk as the non-booting second disk, click Yes, then, after I quit, remove the “WPMac86HD.dsk” file from the trash and drop it on me to set it as the second disk." buttons {"Yes, move to trash", "No, leave it in the app"} default button 1 with title msgTitle

if button returned of result is "Yes, move to trash" then

try

set oldPathAlias to (POSIX file oldPath) as alias

on error

display dialog "Can't create alias for old boot disk." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end try

tell application "Finder"

try

delete oldPathAlias

on error

tell me to activate

display dialog "Error moving old disk image file to the trash." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end try

end tell

end if

end if

tell me to activate

display dialog "The disk image file that you dropped on me will now be the boot disk image in the WPMacApp." & return & return & "Please restart me." & return & return & "If I fail to boot completely, press Ctrl-Esc to force me to quit and start me again." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end if

-- keep existing and add new

else if button returned of result is "Keep existing and add new" then

set copyNewDisk to 0

display dialog "Do you want me to copy the “" & dskName & "” disk image into the WPMacApp application bundle or leave it where it is and link it to application?" buttons {"Copy disk to app", "Leave it and link it", "Cancel"} default button 3 with title msgTitle

if button returned of result is "Cancel" then

error number -128

else if button returned of result is "Copy disk to app" then

set copyNewDisk to 1

end if

tell application "Finder" to set thisPosixName to POSIX path of (i as alias)

set macSystem to (path to me as text) & "Contents:Resources:Files:MacOS.sheepvm" as string

set macSystem to macSystem as alias

-- set oldPath to ((POSIX path of macSystem) & "WPMac86HD.dsk")

set secondDiskName to "SecondDisk.dsk"

set moveNewPath to ((POSIX path of macSystem) & secondDiskName)

set oldDiskExists to 1

try

do shell script "test -e " & quoted form of moveNewPath

on error

set oldDiskExists to 0

end try

if oldDiskExists is 1 then

display dialog "A second disk image already exists. If you click OK I will move it to the trash and set the “" & dskName & "” disk image as the second disk." buttons {"OK", "Cancel"} default button 2 with title msgTitle

if button returned of result is "Cancel" then

error number -128

end if

try

set moveNewPathAlias to (POSIX file moveNewPath) as alias

on error

display dialog "Error setting old file as alias."

end try

tell application "Finder"

try

delete moveNewPathAlias

on error

tell me to activate

display dialog "Error moving disk image file to the trash." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end try

end tell

display dialog "Old second disk image moved to trash." buttons {"OK"} with title msgTitle giving up after 10

end if

(*

try

do shell script "mv " & quoted form of oldPath & space & quoted form of renamePath

on error

tell me to activate

display dialog "Problem renaming old disk image file. Quitting." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end try

*)

if copyNewDisk is 1 then

tell me to activate

display dialog "Please wait a few seconds while I copy the file." & return & return & "I will reappear when the copy is complete." buttons {"OK"} with title msgTitle giving up after 2

try

do shell script "cp " & quoted form of thisPosixName & space & quoted form of moveNewPath

on error

-- do shell script "mv " & quoted form of renamePath & space & quoted form of oldPath

tell me to activate

display dialog "Problem transferring new disk image file. Quitting." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end try

end if

set prefsFile to (path to me as string) & "Contents:Resources:Files:MacOS.sheepvm:prefs" as string

set prefsFile to prefsFile as alias

set prefsPath to (POSIX path of prefsFile) as string

set savePath to ((POSIX path of prefsFile) & ".storage")

set tempPath to ((POSIX path of prefsFile) & ".tempfile")

set diskPath to ((POSIX path of prefsFile) & ".diskfile")

-- write prefs file

try

do shell script "cp " & quoted form of prefsPath & " " & quoted form of savePath

on error

set dialogText to "Problem backing up preferences file. No changes made."

end try

do shell script "grep -v '^disk' " & quoted form of prefsPath & " > " & quoted form of tempPath

do shell script "echo disk WPMac86HD.dsk > " & quoted form of diskPath

if copyNewDisk is 1 then

do shell script "echo disk " & secondDiskName & " >> " & quoted form of diskPath

else

do shell script "echo disk " & thisPosixName & ">> " & quoted form of diskPath

end if

do shell script "cat " & quoted form of diskPath & space & quoted form of tempPath & " > " & quoted form of prefsPath

do shell script "rm " & quoted form of diskPath

do shell script "rm " & quoted form of tempPath

do shell script "rm " & quoted form of savePath

tell me to activate

display dialog "The disk image file that you dropped on me will now be the second (non-booting) disk in the WPMacApp." & return & return & "Please restart me." & return & return & "If I fail to boot completely, press Ctrl-Esc to force me to quit and start me again." buttons {"OK"} with title msgTitle giving up after 10

error number -128

end if

end transferImage


on readPlist() -- put in variable here

set myPlist to "org.wpdos.wpmacapp"

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

set runOnceFlag to ""

set writeDataPath to (quoted form of (POSIX path of (path to documents folder)))

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" & space & myPlist & space & "RunOnce" & space & "true"

set firstRun to true

my showFirstRunMsg()

end try

if runOnceFlag is "false" then

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

set firstRun to true

my showFirstRunMsg()

end if

set printPDFprompt to false

try

set printPDFprompt to value of property list item "printPDFprompt" of property list file myPlistfile

on error

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

end try

try

set dataPath to value of property list item "DataPath" of property list file myPlistfile

on error

-- set writeDataPath to (quoted form of (POSIX path of (path to documents folder)))

do shell script "defaults write" & space & myPlist & space & "DataPath" & space & writeDataPath

end try

try

set defaultWidth to value of property list item "Default Width" of property list file myPlistfile

on error

do shell script "defaults write" & space & myPlist & space & "DefaultWidth" & space & "768"

end try

try

set defaultHeight to value of property list item "DefaultHeight" of property list file myPlistfile

on error

do shell script "defaults write" & space & myPlist & space & "DefaultHeight" & space & "1024"

end try

try

set forceFullScreen to value of property list item "ForceFullScreen" of property list file myPlistfile

on error

do shell script "defaults write" & space & myPlist & space & "ForceFullScreen" & space & "0"

end try

try

set wpStartup to value of property list item "WPStartup" of property list file myPlistfile

on error

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

set wpStartup to "true"

end try

end tell

end readPlist


on setOptions()

set printPDFpromptName to "Set prompting option for default printing system"

set printOptionName to "Set WPMacApp printing options (10.6 and 10.7 only)"

set ipSetupOptionName to "Set up networked PostScript printer for WPMac"

set screenOptionName to "Set WPMacApp screen size options"

set unixOptionName to "Select OS X folder to use as the WPMacApp “Unix” folder"

set startWPName to "Start or don't start WPMac when WPMacApp launches"

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

set optionsList to {printPDFpromptName, ipSetupOptionName, screenOptionName, unixOptionName, startWPName, readmeOptionName}

if Mountain is 0 then

set the beginning of optionsList to printOptionName

end if

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 printPDFpromptName then

my printPDFsetup()

else if doOption contains printOptionName then

set showPrinterMenu to true

tell me to activate

display dialog "The printer setup menu will appear after you click “Finished setting options” on the option settings menu." buttons {"OK"} with title msgTitle giving up after 10

my setOptions()

else if doOption contains ipSetupOptionName then

set doIPSetup to true

tell me to activate

display dialog "The networked PostScript printer setup menu will appear after you click “Finished setting options” on the option settings menu." buttons {"OK"} with title msgTitle giving up after 10

my setOptions()

else if doOption contains screenOptionName then

my screenSizeMenu()

else if doOption contains unixOptionName then

set extraString to ""

set fromWhere to "From setOptions"

my askforUnix(extraString, fromWhere)

else if doOption contains startWPName then

my startWPOption()

else if doOption contains readmeOptionName then

set readmeFromWhere to "From setOptions"

my copyReadMeToDesktop(readmeFromWhere)

end if

end setOptions


on printPDFsetup()

if printPDFprompt is "true" then

set buttonDefault to 1

set printPDFtext to "Yes"

else

set buttonDefault to 2

set printPDFtext to "No"

end if

tell me to activate

display dialog "Do you want the default printing system to ask whether to print to the default OS X printer or create a PDF file in OS X?" & return & return & "If you answer No, the system will print to the default OS X printer, and you can create PDF files by using a PDF macro in WordPerfect." & return & return & "The current setting is " & printPDFtext & "." buttons {"Yes", "No"} default button buttonDefault with title msgTitle

if button returned of result is "Yes" then

try

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

on error errMsg

tell me to activate

display dialog errMsg

end try

else if button returned of result is "No" then

try

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

on error errMsg

tell me to activate

display dialog errMsg

end try

end if

my setOptions()

end printPDFsetup


on startWPOption()

if wpStartup is "true" then

set buttonDefault to 1

set wpStartText to "Yes"

else if wpStartup is "false" then

set buttonDefault to 2

set wpStartText to "No"

end if

tell me to activate

display dialog "Do you want WordPerfect to start automatically when the WPMacApp starts up?" & return & return & "This option is currently set to" & space & wpStartText & "." buttons {"Yes", "No"} default button buttonDefault with title msgTitle

if button returned of result is "Yes" then

if wpStartup is "false" then

set wpStartup to "true"

try

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

on error errMsg

tell me to activate

display dialog errMsg

end try

end if

else if button returned of result is "No" then

if wpStartup is "true" then

set wpStartup to "false"

try

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

on error errMsg

tell me to activate

display dialog errMsg

end try

end if

end if

my setOptions()

end startWPOption


on allPrinterSetup()

if showPrinterMenu is true then

if doIPSetup is true then

tell me to activate

display dialog "I can only prepare WPMac to use either an OS X printer or a networked PostScript printer but not both at the same time." & return & return & "Which do you want to set up now?" & return & return & "You may set up a different printer when you next launch me." buttons {"Set up OS X printer", "Set up networked PostScript printer", "Cancel"} default button 1 with title msgTitle

if button returned of result is "Set up OS X printer" then

set doIPSetup to false

else if button returned of result is "Set up networked PostScript printer" then

set showPrinterMenu to false

else if button returned of result is "Cancel" then

return

end if

my allPrinterSetup()

end if

set ptrFromWhere to "From allPrinterSetup"

my printerPrompt(ptrFromWhere)

end if

if doIPSetup is true then

my prepareIPPrinter()

end if

end allPrinterSetup


on screenSizeMenu()

set prefsFile to (path to me) & "Contents:Resources:Files:MacOS.sheepvm:prefs" as string

set prefsFile to prefsFile as alias

set prefsUnixFile to quoted form of ((POSIX path of prefsFile) as string)

try

set curSize to do shell script "grep '^screen win\\/' " & prefsUnixFile & " | sed 's/^screen win\\///'"

on error

tell me to activate

display dialog "Error reading existing preferences file."

error number -128

end try

-- replace "/" with "x" to match menu display

set curMenu to searchReplaceText("/", "x", curSize)

set calculateSize to 0

set forceFullScreen to 0

set defaultScreenSize to 1

set sizeList to {"640x480", "800x600", "896x762", "1024x768"}

tell application "Image Events" to set numberOfDisplays to count displays

if numberOfDisplays is greater than 1 then

set ifLarge to space & "largest" & space

else

set ifLarge to space

end if

set dspsizes to getDspSizes()

set widthList to {}

set heightList to {}

repeat with thisItem in dspsizes

set the end of widthList to first item in thisItem as number

set the end of heightList to second item in thisItem as number

end repeat

set sortedWidths to simple_sort(widthList)

set sortedHeights to simple_sort(heightList)

set maxWidth to the first item in sortedWidths as number

set maxHeight to the first item in sortedHeights as number

-- set macSize to (maxWidth as string) & "x" & (maxHeight as string)

if maxWidth is greater than 1024 then

if maxHeight is greater than 800 then

set end of sizeList to "1152x870"

end if

end if

if maxWidth is greater than 1280 then

if maxHeight is greater than 800 then

set end of sizeList to "1280x1024"

end if

end if

if maxWidth is greater than 1599 then

set end of sizeList to "1600x1024"

if maxHeight is greater than 1199 then

set end of sizeList to "1600x1200"

end if

end if

set documentSize to "Document size (640x742)"

set landscapeSize to "Landscape, proportioned to OS X screen size"

set portraitSize to "Portrait, proportioned to OS X screen size"

set fullScreenSize to "Full-screen, same size as OS X screen"

set nameList to {documentSize, landscapeSize, portraitSize, fullScreenSize}

set screenSizeList to sizeList & nameList

set getSize to choose from list screenSizeList with prompt "Select a screen size for the WPMacApp" & return & return & "Current size is " & curMenu OK button name "Select" cancel button name "Cancel" with title msgTitle

if getSize is "Cancel" then

my setOptions()

else if getSize contains "1600x1200" then

set defaultWidth to 1600

set defaultHeight to 1200

else if getSize contains "1280x1024" then

set defaultWidth to 1280

set defaultHeight to 1024

else if getSize contains "1152x870" then

set defaultWidth to 1152

set defaultHeight to 870

else if getSize contains "1024x768" then

set defaultWidth to 1024

set defaultHeight to 768

else if getSize contains "896x762" then

set defaultWidth to 896

set defaultHeight to 762

else if getSize contains "800x600" then

set defaultWidth to 800

set defaultHeight to 600

else if getSize contains "640x480" then

set defaultWidth to 640

set defaultHeight to 480

else if getSize contains documentSize then

set defaultWidth to 640

set defaultHeight to 742

else if getSize contains landscapeSize then

set defaultScreenSize to 0

set calculateSize to 1

else if getSize contains portraitSize then

set defaultScreenSize to 0

set calculateSize to 2

else if getSize contains fullScreenSize then

set defaultScreenSize to 0

set forceFullScreen to 1

tell application "System Events"

if Mavericks is 0 then

if UI elements enabled then

else

my setupUniversalAccess()

end if

else

my setupMavericksAccess()

end if

end tell

end if

do shell script "defaults write" & space & myPlist & space & "DefaultWidth" & space & defaultWidth

do shell script "defaults write" & space & myPlist & space & "DefaultHeight" & space & defaultHeight

my setOptions()

end screenSizeMenu


on showFirstRunMsg()

-- offer to copy readme first to desktop, if you want to set options, hold down option key, etc.

tell me to activate

set copyPrompt to "Copy PDF to desktop"

display dialog "This seems to be the first time you have run this version of the WPMac Appliance." & return & return & "I strongly recommend that you study the “Read Me First.pdf” file before you transfer files to and from the WPMacApp and before printing from WordPerfect. I can copy this file to your OS  X desktop and continue to open the WPMacApp." & return & return & "Or I can continue to launch the WPMacApp without copying the “Read Me First.pdf” document." & return & return & "Another copy of the “Read Me First.pdf” document may be found on the desktop of the WPMacApp." with title msgTitle buttons {copyPrompt, "Continue without copying", "Quit"} default button 1

if button returned of result contains copyPrompt then

set readmeFromWhere to "From ShowFirstRunMsg"

my copyReadMeToDesktop(readmeFromWhere)

else if button returned of result is "Quit" then

error number -128

end if

end showFirstRunMsg


on copyReadMeToDesktop(readmeFromWhere)

set thisApp to path to me as text

set readmePDFFile to (thisApp & "Contents:Resources:Files:Read Me First.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 & "WPMacApp Read Me First.pdf")

set pdfReadMeExists to true

try

do shell script "ls -a " & pdfTargetPosix

on error

set pdfReadMeExists to false

end try

-- display dialog pdfReadMeExists

if pdfReadMeExists is true then

tell me to activate

display dialog "The WPMacApp “Read Me First.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 “WPMacApp 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 “WPMacApp 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


on searchReplaceText(searchTerm, replaceTerm, theText)

set searchTerm to searchTerm as list

set replaceTerm to replaceTerm as list

set theText to theText as text

set oldTID to AppleScript's text item delimiters

repeat with i from 1 to count searchTerm

set AppleScript's text item delimiters to searchTerm's item i

set theText to theText's text items

set AppleScript's text item delimiters to replaceTerm's item i

set theText to theText as text

end repeat

set AppleScript's text item delimiters to oldTID

return theText

end searchReplaceText


on setupLaunchDaemon()

-- remove folder action script

set userPath to path to home folder as text

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

set libPosix to POSIX path of libPath

set fasPosix to libPosix & "Scripts/Folder Action Scripts/WPTransferFolderScript.scpt"

tell application "System Events"

set dataPath to value of property list item "DataPath" of property list file myPlistfile

end tell

set printPath to (dataPath & ".WPTransfer") as text

-- remove old folder action script system

(* -- not needed, already done above?

try

do shell script "/bin/mkdir -p " & quoted form of dataPath & "/.WPTransfer"

end try

try

do shell script "ls" & space & quoted form of (POSIX path of userFldrPath & fldrScript)

end try

*)

try

tell application "System Events" to set FAScripts to name of every script of folder action ".WPTransfer"

repeat with EachScript in FAScripts

set ScriptName to contents of EachScript

tell application "System Events" to delete script ScriptName of folder action ".WPTransfer"

end repeat

--If we deleted all the scripts, delete the folder action object as well

tell application "System Events" to delete folder action ".WPTransfer"

end try

try

do shell script "rm" & space & quoted form of fasPosix

end try

--debug

--this repeats routine in setupPaths but may be needed anyway:

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

set filesFolder to contentsFldr as alias

set filesPosix to (POSIX path of filesFolder)

set scriptPosix to (filesPosix & "wpmacappfolder.scpt")

set daemonPosix to (filesPosix & "org.wpdos.wpmacfolder.plist")

try

do shell script "rm " & quoted form of daemonPosix

end try

do shell script "cp '" & filesPosix & "plisthead.txt' '" & filesPosix & "org.wpdos.wpmacfolder.plist'"

do shell script "echo \\<string\\>" & scriptPosix & "\\</string\\>>>'" & filesPosix & "org.wpdos.wpmacfolder.plist'"

do shell script "cat '" & filesPosix & "plistmiddle.txt' >>'" & filesPosix & "org.wpdos.wpmacfolder.plist'"

do shell script "echo \\<string\\>" & posixFolder & "\\</string\\>>>'" & filesPosix & "org.wpdos.wpmacfolder.plist'"

do shell script "cat '" & filesPosix & "plistfoot.txt'>>'" & filesPosix & "org.wpdos.wpmacfolder.plist'"

try

do shell script "launchctl remove org.wpdos.wpmacfolder"

end try

do shell script "launchctl load " & quoted form of daemonPosix

set sswp to false

end setupLaunchDaemon


on setupSSWPLaunchDaemon()

-- remove folder action script

set userPath to path to home folder as text

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

set libPosix to POSIX path of libPath

set scriptPosix to libPosix & "Scripts/Folder Action ScriptsSheepShaver Print-PDF 1.4.1.scpt"

tell application "System Events"

set dataPath to value of property list item "DataPath" of property list file myPlistfile

end tell

set sswpPrintPath to (dataPath & "SheepShaver Data/SS > OSX") as text

set sswpPrintString to (dataPath & "SheepShaver Data/SS \\> OSX") as text

-- remove old folder action script system

(*

try

do shell script "mkdir -p " & quoted form of sswpPrintPath

end try

try

do shell script "ls" & space & quoted form of (POSIX path of userFldrPath & fldrScript)

end try

*)

-- not sure which of these two is needed:

try

tell application "System Events" to set FAScripts to name of every script of folder action "SheepShaver Data"

repeat with EachScript in FAScripts

set ScriptName to contents of EachScript

tell application "System Events" to delete script ScriptName of folder action "SheepShaver Data"

end repeat

--If we deleted all the scripts, delete the folder action object as well

tell application "System Events" to delete folder action "SheepShaver Data"

end try

try

tell application "System Events" to set FAScripts to name of every script of folder action "SS > OSX"

repeat with EachScript in FAScripts

set ScriptName to contents of EachScript

tell application "System Events" to delete script ScriptName of folder action "SS > OSX"

end repeat

--If we deleted all the scripts, delete the folder action object as well

tell application "System Events" to delete folder action "SS > OSX"

end try

(*

try

do shell script "rm" & space & quoted form of actionPosix

end try

*)

--debug

--this repeats routine in setupPaths but may be needed anyway:

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

set filesFolder to contentsFldr as alias

set filesPosix to (POSIX path of filesFolder)

set scriptPosix to (filesPosix & "SSWPprintpdf.scpt")

set daemonPosix to (filesPosix & "org.wpmacyahoogroup.sswpfolder.plist")

try

do shell script "rm " & quoted form of daemonPosix

end try

do shell script "cp '" & filesPosix & "plisthead-sswp.txt' '" & filesPosix & "org.wpmacyahoogroup.sswpfolder.plist'"

do shell script "echo \\<string\\>" & scriptPosix & "\\</string\\>>>'" & filesPosix & "org.wpmacyahoogroup.sswpfolder.plist'"

do shell script "cat '" & filesPosix & "plistmiddle.txt' >>'" & filesPosix & "org.wpmacyahoogroup.sswpfolder.plist'"

do shell script "echo \\<string\\>" & sswpPrintString & "\\</string\\>>>'" & filesPosix & "org.wpmacyahoogroup.sswpfolder.plist'"

do shell script "cat '" & filesPosix & "plistfoot.txt'>>'" & filesPosix & "org.wpmacyahoogroup.sswpfolder.plist'"

try

do shell script "launchctl remove org.wpmacyahoogroup.sswpfolder"

end try

do shell script "launchctl load " & quoted form of daemonPosix

set sswp to false

end setupSSWPLaunchDaemon