Save image in SheepShaver clipboard to a PNG file

About SheepShaver, a PPC Mac emulator for Windows, MacOS X, and Linux that can run System 7.5.3 to MacOS 9.0.4.

Moderators: Cat_7, Ronald P. Regensburg, ClockWise

Post Reply
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Save image in SheepShaver clipboard to a PNG file

Post by emendelson »

As discussed in another thread ( viewtopic.php?f=20&t=11314 ), if you save an image to the clipboard in SheepShaver, the host macOS clipboard (in recent macOS versions) won't be able to paste the image into a host application. Thanks to mabam on this forum, there's now a macOS AppleScript that will convert the image data in the host clipboard to a format that can the host clipboard can paste, but this requires running the AppleScript on the host after saving the image to the clipboard in SheepShaver.

Here is an alternative method of getting an image from the SheepShaver clipboard to the macOS host. It is an AppleScript that uses the GIFConverter application in SheepShaver to save the clipboard to a PNG file on the SheepShaver desktop (by default), and you can drag it from there to the Unix folder. If you use the string "Host" in the name of the AppleScript application, it moves the PNG file to the UNIX folder for you, and uses the current date and time in the filename so that you don't have to worry about overwriting an existing file.

The AppleScript is clumsy and inefficient because I still don't know how to write good code, but it seems to work, and someone may find it useful. You can find GIFConverter (together with a registration code) on Macintosh Garden; install, run it, and enter the registration code before trying to use this script.

Code: Select all

-- Clip Image to PNG
-- uses GIFConverter to save an image in the clipboard to a PNG file on the SheepShaver desktop
-- if the app has the strong "Host" in its name, the file will be saved to the Unix folder

on run {}
	set myName to path to me as string
	set sendToHost to false
	if myName contains "Host" then
		set sendToHost to true
	end if
	set clipInfo to (clipboard info) as string
	if clipInfo contains "picture" then
		set deskDir to path to desktop folder as string
		set saveImage to "FromClipboard.png"
		set saveFile to deskDir & saveImage
		my convertFile(saveImage, saveFile)
		if sendToHost is true then
			my moveToHost(saveImage, saveFile)
		end if
	else
		activate
		display dialog "No image in clipboard." buttons {"OK"} giving up after 2
	end if
	error number -128
end run

on convertFile(saveImage, saveFile)
	tell application "Finder"
		if exists file saveFile then
			activate
			display dialog saveImage & " already exists on the desktop." & return & return & "Delete and replace?" buttons {"Yes", "No"} default button 2
			if button returned of result is "Yes" then
				delete file saveFile
			else
				error number -128
			end if
		end if
	end tell
	
	set appRunning to false
	if (list processes) contains "GIFConverter" then
		set appRunning to true
	end if
	
	tell application "GIFConverter"
		activate
		make at beginning new graphic document
		tell image 1 of graphic document 1
			paste
		end tell
		try
			close palette 1
		end try
		save graphic document 1 in file saveFile as PNG
		try
			close graphic document 1 saving no
		end try
		if appRunning is false then
			try
				quit application "GIFConverter"
			end try
		end if
	end tell
end convertFile

on moveToHost(saveImage, saveFile)
	set dateString to my getDateString()
	set hostFile to "Unix:" & dateString & ".png"
	set saveFile to file saveFile as alias
	set hostFolder to "Unix:" as alias
	tell application "Finder"
		set hostExists to true
		try
			set hostFile to ("Unix:" & saveImage) as alias
		on error
			set hostExists to false
		end try
		if hostExists is true then
			activate
			display dialog saveImage & " already exists in the shared Unix folder." & return & return & "Delete and replace?" buttons {"Yes", "No"} default button 2
			if button returned of result is "Yes" then
				duplicate saveFile to hostFolder with replacing
			end if
		else
			duplicate saveFile to hostFolder
		end if
		delete file saveFile
	end tell
end moveToHost

on getDateString()
	set TID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	set right_now to current date
	set the_time to (time string of right_now) as string -- something like "4:20:56 PM"
	set am_pm to text -2 thru -1 of the_time -- is it AM or PM
	set the_hour to text item 1 of the_time as string -- in this example, "4"
	set the_minute to text item 2 of the_time as string -- in this example, "20"
	set the_seconds to text 1 thru 2 of text item 3 of the_time as string
	if (am_pm is "PM") then
		set the_hour to the_hour + 12
	end if
	if the_hour is "24" then
		set the_hour to "12"
	end if
	set AppleScript's text item delimiters to TID
	tell (current date) to set dateString to ((its year as text) & (its month) & its day & "-" & ¬
		the_hour & "." & the_minute & "." & the_seconds as string)
	return dateString as string
end getDateString
Post Reply