Copy a PNG image to clipboard on OSX

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Copy a PNG image to clipboard on OSX

Maarten Mostert-2
Hi,

I managed to solve this old problem using the following Applescript which opens a png file written in User’s Library directory of my application.

set aHome to ":Users"
set aUser to system attribute "USER"
set aFileName to "Library:StakePoint:stakepointclipboard.png"
set totalName to aHome & ":" & aUser & ":" & aFileName
set pngData to read file totalName as «class PNGf»
set the clipboard to pngData

The total method doing the thing for both Windows and OSX then shows off as follows.

Notice that the applescript is compiled and available from the app framework directory.
The actual png file is written out of the hidden User’s Library directory of my application.


 createCopyPngImage

| viewBounds progressImage progressCR myNewPixmap ws compStrings aFilename aString componentStrings |
viewBounds := parent avancementCairoViewWidget bounds.
progressImage := CairoGraphics.ImageSurface
format: CairoGraphics.CairoFormat argb32
extent: viewBounds width rounded @ viewBounds height rounded.
progressCR := progressImage context.
parent avancementCairoViewWidget renderOn: progressCR.
Screen default platformName asSymbol = #'MS-Windows'
ifTrue: 
[myNewPixmap := Pixmap extent: progressImage extent.
progressImage displayOn: myNewPixmap graphicsContext.
myNewPixmap toClipboard.
MyDialog warn: (UserMessage defaultString: 'Image copied to clipboard'
key: #imageCopiedInClipBoard)]
ifFalse: 
[Screen default platformName asSymbol = #MacOSX
ifFalse: [self error: 'UNIX']
ifTrue: 
[ws := ByteArray new writeStream.
progressImage writeToPngStream: ws.
compStrings := self getMakeLocalAppDataDirectory componentStrings.
compStrings add: 'stakepointclipboard.png'.
aFilename := (LogicalFilename fromComponents: compStrings) asFilename.
(aFilename writeStream)
binary;
nextPutAll: ws contents;
close.
componentStrings := CEnvironment commandLine first
asLogicalFileSpecification componentStrings.
componentStrings removeLast.
componentStrings removeLast.
componentStrings add: 'Frameworks'.
componentStrings add: 'copytoclipboard.app'.
aString := (LogicalFilename fromComponents: componentStrings) asString.
ExternalProcess shOne: aString.
ExternalProcess
execute: 'open'
arguments: (Array with: aString)
do: 
[MyDialog warn: (UserMessage defaultString: 'Image copied to clipboard'
key: #imageCopiedInClipBoard)]
errorStreamDo: [:error | ]]]


where:


getMakeLocalAppDataDirectory

| localAppData |
Screen default platformName asSymbol = #'MS-Windows'
ifTrue: 
[localAppData := (OSSystemSupport concreteClass new
getVariable: 'LOCALAPPDATA') asFilename
asLogicalFileSpecification componentStrings]
ifFalse: 
[Screen default platformName asSymbol = #MacOSX
ifFalse: [Dialog warn: 'I am on UNIX but not OSX'].
localAppData := (OSSystemSupport concreteClass new getVariable: 'HOME')
asFilename asLogicalFileSpecification
componentStrings.
localAppData add: 'Library'].
localAppData add: 'StakePoint'.
(LogicalFilename fromComponents: localAppData) asFilename exists
ifFalse: 
[(LogicalFilename fromComponents: localAppData) asFilename makeDirectory].
^(LogicalFilename fromComponents: localAppData) asLogicalFileSpecification

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc