Can't set width of Image

Get help using and writing Nisus Writer Pro macros.
Post Reply
blueapples
Posts: 4
Joined: 2010-05-06 13:10:28

Can't set width of Image

Post 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
else
	Prompt “Select an Image first!”
end
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?
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Can't set width of Image

Post 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 = Document.active
$sel = $doc.textSelection
$kind = 'inline'

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

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

$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 = $img.name
if $name == ''
	$name = 'unnamed'
end
$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]
else
	exit 'Invalid input, exit...'
end

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

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

$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 = ''
else
	$objReplace = '\x20\x5Cobjscalex' & $x
	$objReplace &= '\x20\x5Cobjscaley' & $y
	$picReplace = '\x20\x5Cpicscalex' & $x
	$picReplace &= '\x20\x5Cpicscaley' & $y
end

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

$image = Decode RTF $image

if $kind == 'floating'
	$doc = Document.new
	$image &= Cast to String "\n"
end

$doc.insertText $image
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Can't set width of Image

Post 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.
Post Reply