Page 1 of 1

Can't set width of Image

Posted: 2010-05-06 13:23:23
by blueapples
The width property of Image objects is defined as setable but I am unable to do so. I have the following macro which I am attempting to use solving an issue with pasted images not scaled to the correct size. The error I get is 'You cannot change the "width" property of the Image object.'

Code: Select all

if Image.selectedImage
	$img = Image.selectedImage
	$img.width = $img.sourceWidth
	$img.height = $img.sourceHeight
	Prompt “Select an Image first!”
Image object notes on the width property show it to be setable:
.width v1.3
Returns the display width (in points) of the image as it appears on the page, taking into account scaling and cropping. Altering this property changes the display width of the image.
Any ideas?

Re: Can't set width of Image

Posted: 2010-05-07 05:46:07
by Kino
Unfortunately the description of those properties is inexact: they are read only. To resize images, we have to do something complicated.

Code: Select all

# Select an image and run the macro. A dialog box will ask you
# to enter integers as a new size (percent).

# If it is a floating graphic, the resized image will appear
# in a new window because a macro cannot replace such an image.

# Although you will find "75 x 75" in the input field, any
# non-digit character works as a separator.

# Tested image formats: bmp, eps, gif, jpeg, jpeg2000, pict,
# pdf, png, psd, sgi, tga and tiff.

Require Pro Version 1.3

$doc =
$sel = $doc.textSelection
$kind = 'inline'

$img = Image.selectedImage
if $img == undefined
	exit 'No image selected, exiting...' 

if $sel != undefined
	$image = Encode RTF $sel.subtext
	Send Selector 'copy:'
	$image = Read Clipboard
	$image = Encode RTF $image
	$kind = 'floating'

$image = Cast to String $image

$x = $img.width / $img.sourceWidth
$y = $img.height / $img.sourceHeight
$x = Cast to Int $x * 100
$y = Cast to Int $y * 100
$points = '[' & $img.width
$points &= '/' & $img.sourceWidth
$points &= ' pt x ' & $img.height
$points &= '/' & $img.sourceHeight
$points &= ' pt]'
$name = $
if $name == ''
	$name = 'unnamed'
$message = "Scale this image ($name) to..."
$detail = "- Actual size: $x x $y % $points ($kind)\n"
$detail &= '- Enter a single value to keep the original proportion '
$detail &= '(e.g. 100 to restore the original size)'

$input = Prompt Input $message, $detail, '', '75 x 75'

$input.findAndReplace '^\D*(\d+)\D+(\d+)?\D*$', '\1|\2', 'E-i$'
$input = $input.split '|'

if $input.count == 1
	$x = $y = Cast to Int $input[0]
elsif $input.count == 2
	$x = Cast to Int $input[0]
	$y = Cast to Int $input[1]
	exit 'Invalid input, exit...'

if ! $x * $y  # then, $x or $y must be zero
	exit 'Invalid input, exit...'

$originalSize = false
if $x == 100
	if $x == $y
		$originalSize = true

$objFind = '(?:\x20\x5Cobjscalex[0-9]+\x20\x5Cobjscaley[0-9]+)?'
$objFind &= '(?=\x20{\x5C\*\x5Cobjdata)'
$picFind = '(?:\x20\x5Cpicscalex[0-9]+\x20\x5Cpicscaley[0-9]+)?'
$picFind &= '(?=\x20\x5C(?:macpict|pngblip|jpegblip))'

if $originalSize == true
	$objReplace = $picReplace = ''
	$objReplace = '\x20\x5Cobjscalex' & $x
	$objReplace &= '\x20\x5Cobjscaley' & $y
	$picReplace = '\x20\x5Cpicscalex' & $x
	$picReplace &= '\x20\x5Cpicscaley' & $y

$image.findAndReplace $objFind, $objReplace, 'E-i'
$image.findAndReplace $picFind, $picReplace, 'E-i'

$image = Decode RTF $image

if $kind == 'floating'
	$doc =
	$image &= Cast to String "\n"

$doc.insertText $image

Re: Can't set width of Image

Posted: 2010-05-17 14:20:48
by martin
The macro language reference is incorrect; the image object width/height properties are read-only. It's something we wanted to get working, but unfortunately didn't have time for.

Kino's RTF munging macro is dirty, but it's the best you can do for now.