UPDATED: 08/10/2020
Pixelmator Pro Script Library.
Any feedback is always welcome.
Regards.
Fredrik
(**
* Description: Script Library for Pixelmator Pro v1.8
*
* Revisions:
* 01/10/2020 - First release
* 08/10/2020 - Update the code of handlers also include new handlers.
*
*
* Legal: For you to use, modify, update and share.
*
* The only thing I ask from you.
* (If you do change/update, share it with everyone).
*)
(**
* [Document suite]:
* makeDocumentWithImageSize:(list), makeDocumentFromClipboard()
* getDocumentInfoProperties()
*)
(**
* [Layer suite]:
* makeLayerWithName:(string), duplicateLayerWithName:(string)
* selectLayerWithName:(string), selectLayerWithNamesInRange:(string):toLayerName:(string)
* deleteLayerWithName:(string), renameLayerNameFrom:(string):toLayerName:(string)
* mergeLayersByIndex:(list), mergeLayersByVisible(), mergeLayersByAll()
* replaceLayerByName:(string):withLayer:(string)
*)
(**
* [Selection suite]
* convertSelectionToShape(), invertSelection(), deselectSelection(),
* fillSelectionByColor:(list), drawSelectionByRectangular:(list):mode:(string),
* drawSelectionByElliptical:(list):mode:(string), resizeSelectionByBounds:(list),
*
* positionByCurrentSelection:(list), resizeByCurrentSelection:(list),
* rotateByCurrentSelection:(integer), boolVisibleByCurrentSelection:(boolean)
*)
(**
* [Shape suite]:
* makeShapeRectangleWithProperties:(string) shapeSize:(list)
* shapePosition:(list) layerPosition:(string) layerPosName:(string)
*
* makeShapeRoundedRectangleWithProperties:(string) shapeSize:(list)
* shapePosition:(list) shapeRadius:(integer) layerPosition:(string) layerPosName:(string)
*
* makeShapeEllipseWithProperties:(string) shapeSize:(list)
* shapePosition:(list) layerPosition:(string) layerPosName:(string)
*
* makeShapePolygonWithProperties:(string) shapeSize:(list)
* shapePosition:(list) layerPosition:(string) layerPosName:(string)
*
* makeShapeStarWithProperties:(string) shapeSize:(list)
* shapePosition:(list) shapePoints:(integer) shapeRadius:(real)
* layerPosition:(string) layerPosName:(string)
*)
(**
* [Style suite]:
* blendmodeStyleInCurrentSelection:(string), shadowStyleInCurrentSelection:(list)
* strokeStyleColorInCurrentSelection:(list), fillStyleColorInCurrentSelection:(list)
* innerShadowStyleColorInCurrentSelection:(list)
*
*)
--- [Document suite]
(**
* [makeDocumentWithImageSize:(list)]
*
* Description: Make new document with image size {width, height}
* ex. my makeDocumentWithImageSize:{512, 512}
*)
-- my makeDocumentWithImageSize:{1024, 1024}
on makeDocumentWithImageSize:_imageSize
tell application "Pixelmator Pro"
try
set {theWidth, theHeight} to _imageSize as list
make new document with properties {width:theWidth, height:theHeight, resolution:300, bits per channel:8}
end try
end tell
end makeDocumentWithImageSize:
(**
* [makeDocumentFromClipboard()]
*
* Description: Make new document from clipboard.
*)
-- makeDocumentFromClipboard()
on makeDocumentFromClipboard()
tell application "Pixelmator Pro"
make document from clipboard
end tell
end makeDocumentFromClipboard
(**
* [getDocumentInfoProperties()]
*
* Description: Get properties of document info of current document.
*)
-- getDocumentInfoProperties()
on getDocumentInfoProperties()
tell application "Pixelmator Pro"
tell front document to its properties of document info
end tell
end getDocumentInfoProperties
--- [Layer suite]
(**
* [makeLayerWithName:(string)]
*
* Description: Make new layer with name string.
* ex. my makeLayerWithName:"Layer1"
*)
-- my makeLayerWithName:"Layer1"
on makeLayerWithName:_nameString
tell application "Pixelmator Pro"
tell front document to make new layer with properties {name:_nameString}
end tell
end makeLayerWithName:
(**
* [duplicateLayerWithName:(string)]
*
* Description: Duplicate a layer with name string.
*)
-- my duplicateLayerWithName:"background"
on duplicateLayerWithName:_nameString
tell application "Pixelmator Pro"
tell front document to duplicate layer _nameString
end tell
end duplicateLayerWithName:
(**
* [selectLayerWithName:(string)]
*
* Description: Select a layer with name string.
*)
-- my selectLayerWithName:"Shape"
on selectLayerWithName:_nameString
tell application "Pixelmator Pro"
tell front document
set theLayerList to its name of every layer
set theIndex to my list_position(_nameString, theLayerList)
return (select layer theIndex)
end tell
end tell
end selectLayerWithName:
(**
* [selectLayerWithNamesInRange:(string):toLayerName:(string)]
*
* Select a layers with name string from top layer to bottom.
*)
-- my selectLayerWithNamesFrom:"Shape" toLayerName:"Background Layer"
on selectLayerWithNamesFrom:_nameStringTop toLayerName:_nameStringBottom
tell application "Pixelmator Pro"
tell front document
set theLayerList to its name of every layer
set theIndexBegin to my list_position(_nameStringTop, theLayerList)
set theIndexEnd to my list_position(_nameStringBottom, theLayerList)
set layerItems to select (layers theIndexBegin thru theIndexEnd)
end tell
end tell
end selectLayerWithNamesFrom:toLayerName:
(**
* [deleteLayerWithName:(string)]
*
* Description: Delete a layer with name string.
*)
-- my deleteLayerWithName:"Shape"
on deleteLayerWithName:_nameString
tell application "Pixelmator Pro"
tell front document to delete layer _nameString
end tell
end deleteLayerWithName:
(**
* [renameLayerNameFrom:(string):toLayerName:(string)]
*
* Description: Rename a layer from name string to a new name string.
*)
-- my renameLayerNameFrom:"Background Layer" toLayerName:"Background"
on renameLayerNameFrom:_nameStringCurrent toLayerName:_nameStringNew
tell application "Pixelmator Pro"
tell front document
set theLayerList to its name of every layer
try
set theIndex to my list_position(_nameStringCurrent, theLayerList)
set name of layer (get item theIndex of theLayerList) to _nameStringNew
end try
end tell
end tell
end renameLayerNameFrom:toLayerName:
(**
* [mergeLayersByIndex:(list)]
*
* Description: Merge 2 layers by its index position. (lower index at top)
*)
-- my mergeLayersByIndex:{1, 4}
on mergeLayersByIndex:_indexInteger
tell application "Pixelmator Pro"
tell front document
try
merge layers {layer ((item 1 of _indexInteger) as integer), layer ((item 2 of _indexInteger) as integer)}
end try
end tell
end tell
end mergeLayersByIndex:
(**
* [mergeLayersByVisible()]
*
* Description: Merge layers by visible.
*)
-- mergeLayersByVisible()
on mergeLayersByVisible()
tell application "Pixelmator Pro"
tell front document to merge visible
end tell
end mergeLayersByVisible
(**
* [mergeLayersByAll()]
*
* Description: Merge layers by all.
*)
-- mergeLayersByAll()
on mergeLayersByAll()
tell application "Pixelmator Pro"
tell front document to merge all
end tell
end mergeLayersByAll
(**
* [replaceLayerByName:(string):withLayer:(string)]
*
* Description: Replace layer by name with other layer.
*)
-- my replaceLayerByName:"Shape" withLayer:"Background"
on replaceLayerByName:_nameString withLayer:_replaceString
tell application "Pixelmator Pro"
tell front document to set layer _nameString to layer _replaceString
end tell
end replaceLayerByName:withLayer:
--- [Selection suite]
(**
* [convertSelectionToShape()]
*
* Description: Convert the selection to shape
*)
-- convertSelectionToShape()
on convertSelectionToShape()
tell application "Pixelmator Pro"
tell front document to convert selection into shape
end tell
end convertSelectionToShape
(**
* Invert selection of the object.
*)
-- invertSelectionObject()
on invertSelectionObject()
tell application "Pixelmator Pro"
tell front document to invert selection
end tell
end invertSelectionObject
(*
* [deselectSelection()]
*
* Description: Deselect the selection of the object.
*)
-- deselectSelection()
on deselectSelection()
tell application "Pixelmator Pro"
tell front document to deselect
end tell
end deselectSelection
(**
* [fillSelectionByColor:(list)]
*
* Description: Fill the selection by color
*)
-- my fillSelectionByColor:{2400, 35000, 0}
on fillSelectionByColor:_inputColor
tell application "Pixelmator Pro"
tell front document
fill current layer with color _inputColor
end tell
end tell
end fillSelectionByColor:
(**
* [drawSelectionByRectangular:(list):mode:(string)]
*
* Description: Draw rectangular selection object with mode.
* [New], [Add], [Substract] and [Intersect]
*)
-- my drawSelectionByRectangular:{270, 170, 50, 50} mode:"Add"
on drawSelectionByRectangular:_inputBounds mode:_modeString
tell application "Pixelmator Pro"
if (_modeString is not "") then
if (_modeString = "New") then
set modeString to new selection
end if
if (_modeString = "Add") then
set modeString to add selection
end if
if (_modeString = "Subtract") then
set modeString to subtract selection
end if
if (_modeString = "Intersect") then
set modeString to intersect selection
end if
else
error number -128
end if
tell front document to draw selection bounds _inputBounds mode modeString
end tell
end drawSelectionByRectangular:mode:
(**
* [drawSelectionByElliptical:(list):mode:(string)]
*
* Description: Draw elliptical selection object with mode.
*)
-- my drawSelectionByElliptical:{200, 150, 200, 200} mode:"Add"
on drawSelectionByElliptical:_inputBounds mode:_modeString
tell application "Pixelmator Pro"
if (_modeString is not "") then
if (_modeString = "New") then
set modeString to new selection
end if
if (_modeString = "Add") then
set modeString to add selection
end if
if (_modeString = "Subtract") then
set modeString to subtract selection
end if
if (_modeString = "Intersect") then
set modeString to intersect selection
end if
else
error number -128
end if
tell front document to draw elliptical selection bounds _inputBounds mode modeString
end tell
end drawSelectionByElliptical:mode:
(**
* [resizeSelectionByBounds:(list)]
*
* Description: Resize the bounds with
* coordinate {TopLeft_X:integer, Y:integer, bottomRight_X:integer, Y:integer}
*)
-- my resizeSelectionByBounds:{200, 200, 400, 400}
on resizeSelectionByBounds:_theRecord
tell application "Pixelmator Pro"
tell front document to tell current layer
set bounds to _theRecord
end tell
end tell
end resizeSelectionByBounds:
(*
* [positionByCurrentSelection:(list)]
*
* Description: Position of the selection of current selected layer in {x,y} as list
* coordinate {TopLeft_X:integer,TopLeft_Y:integer}
*)
-- my positionByCurrentSelection:{300, 300}
on positionByCurrentSelection:_theRecord
tell application "Pixelmator Pro"
tell front document to tell current layer
set position to _theRecord as list
end tell
end tell
end positionByCurrentSelection:
(*
* [resizeByCurrentSelection:(list)]
*
* Description: Resize the selection of current selected layer as list
*)
-- my resizeByCurrentSelection:{150, 150}
on resizeByCurrentSelection:_theRecord
tell application "Pixelmator Pro"
tell front document to tell current layer
set {width, height} to _theRecord as list
end tell
end tell
end resizeByCurrentSelection:
(**
* [boolVisibleByCurrentSelection:(boolean)]
*
* Description: Set the boolean value of visible of curent selected layer.
*)
-- my boolVisibleByCurrentSelection:true
on boolVisibleByCurrentSelection:_Boolean
tell application "Pixelmator Pro"
tell front document to tell current layer
set visible to _Boolean as boolean
end tell
end tell
end boolVisibleByCurrentSelection:
(**
* [rotateByCurrentSelection:(integer)]
*
* Description: Rotation of the selection of current selected layer as integer
*)
-- my rotateByCurrentSelection:25
on rotateByCurrentSelection:_rotateInteger
tell application "Pixelmator Pro"
tell front document to tell current layer
set rotation to _rotateInteger as integer
end tell
end tell
end rotateByCurrentSelection:
--- [Shape suite]
(**
* [makeShapeRectangleWithProperties:(string):shapeSize:(list):shapePosition:(list):layerPosition:(string):layerPosName:(string)]
*
* Description: Make shape rectangle with properties in 4 type of layer position
* ([begin] and [end] do not use layerPosName) [before] and [after]
*)
-- my makeShapeRectangleWithProperties:"Rect1" shapeSize:{100, 100} shapePosition:{350, 200} layerPosition:"end" layerPosName:""
on makeShapeRectangleWithProperties:_shapeName shapeSize:_Size shapePosition:_Pos layerPosition:_layerPosition layerPosName:_layerPosName
tell application "Pixelmator Pro"
tell front document
try
set {theWidth, theHeight} to _Size as list
set theProperties to {name:_shapeName, position:_Pos as list, width:theWidth, height:theHeight}
if (_layerPosName is "") then -- we do not use layerPosName.
if (_layerPosition is "begin") then
make new rectangle shape layer at the beginning of layers with properties theProperties
end if
if (_layerPosition is "end") then
make new rectangle shape layer at the end of layers with properties theProperties
end if
end if
if (_layerPosition is "before") then
make new rectangle shape layer at before layer _layerPosName with properties theProperties
end if
if (_layerPosition is "after") then
make new rectangle shape layer at after layer _layerPosName with properties theProperties
end if
end try
end tell
end tell
end makeShapeRectangleWithProperties:shapeSize:shapePosition:layerPosition:layerPosName:
(**
* [makeShapeRoundedRectangleWithProperties:(string):shapeSize:(list):shapePosition:(list):layerPosition:(string):layerPosName:(string)]
*
* Description: Make shape rounded rectangle with properties in 4 type of layer position
* ([begin] and [end] do not use layerPosName) [before] and [after]
*)
-- my makeShapeRoundedRectangleWithProperties:"roundedRect2" shapeSize:{200, 200} shapePosition:{200, 100} shapeRadius:35 layerPosition:"begin" layerPosName:""
on makeShapeRoundedRectangleWithProperties:_shapeName shapeSize:_Size shapePosition:_Pos shapeRadius:_Radius layerPosition:_layerPosition layerPosName:_layerPosName
tell application "Pixelmator Pro"
tell front document
try
set {theWidth, theHeight} to _Size as list
set theProperties to {name:_shapeName, position:_Pos as list, width:theWidth, height:theHeight, corner radius:_Radius as integer}
if (_layerPosName is "") then -- we do not use layerPosName.
if (_layerPosition is "begin") then
make new rounded rectangle shape layer at the beginning of layers with properties theProperties
end if
if (_layerPosition is "end") then
make new rounded rectangle shape layer at the end of layers with properties theProperties
end if
end if
if (_layerPosition is "before") then
make new rounded rectangle shape layer at before layer _layerPosName with properties theProperties
end if
if (_layerPosition is "after") then
make new rounded rectangle shape layer at after layer _layerPosName with properties theProperties
end if
end try
end tell
end tell
end makeShapeRoundedRectangleWithProperties:shapeSize:shapePosition:shapeRadius:layerPosition:layerPosName:
(**
* [makeShapeEllipseWithProperties:(string):shapeSize:(list):shapePosition:(list):layerPosition:(string):layerPosName:(string)]
*
* Description: Make shape ellipse with properties in 4 type of layer position
* ([begin] and [end] do not use layerPosName) [before] and [after]
*)
-- my makeShapeEllipseWithProperties:"Shape1" shapeSize:{100, 100} shapePosition:{300, 200} layerPosition:"begin" layerPosName:""
on makeShapeEllipseWithProperties:_shapeName shapeSize:_Size shapePosition:_Pos layerPosition:_layerPosition layerPosName:_layerPosName
tell application "Pixelmator Pro"
tell front document
try
set {theWidth, theHeight} to _Size as list
set theProperties to {name:_shapeName, position:_Pos as list, width:theWidth, height:theHeight}
if (_layerPosName is "") then -- we do not use layerPosName.
if (_layerPosition is "begin") then
make new ellipse shape layer at the beginning of layers with properties theProperties
end if
if (_layerPosition is "end") then
make new ellipse shape layer at the end of layers with properties theProperties
end if
end if
if (_layerPosition is "before") then
make new ellipse shape layer at before layer _layerPosName with properties theProperties
end if
if (_layerPosition is "after") then
make new ellipse shape layer at after layer _layerPosName with properties theProperties
end if
end try
end tell
end tell
end makeShapeEllipseWithProperties:shapeSize:shapePosition:layerPosition:layerPosName:
(**
* [makeShapePolygonWithProperties:(string):shapeSize:(list):shapePosition:(list):layerPosition:(string):layerPosName:(string)]
*
* Description: Make shape polygon with properties in 4 type of layer position
* ([begin] and [end] do not use layerPosName) [before] and [after]
*)
-- my makeShapePolygonWithProperties:"Poly" shapeSize:{200, 200} shapePosition:{200, 200} layerPosition:"begin" layerPosName:""
on makeShapePolygonWithProperties:_shapeName shapeSize:_Size shapePosition:_Pos layerPosition:_layerPosition layerPosName:_layerPosName
tell application "Pixelmator Pro"
tell front document
try
set {theWidth, theHeight} to _Size as list
set theProperties to {name:_shapeName, position:_Pos as list, width:theWidth, height:theHeight}
if (_layerPosName is "") then -- we do not use layerPosName.
if (_layerPosition is "begin") then
make new polygon shape layer at the beginning of layers with properties theProperties
end if
if (_layerPosition is "end") then
make new polygon shape layer at the end of layers with properties theProperties
end if
end if
if (_layerPosition is "before") then
make new polygon shape layer at before layer _layerPosName with properties theProperties
end if
if (_layerPosition is "after") then
make new polygon shape layer at after layer _layerPosName with properties theProperties
end if
end try
end tell
end tell
end makeShapePolygonWithProperties:shapeSize:shapePosition:layerPosition:layerPosName:
(**
* [makeShapeStarWithProperties:(string):shapeSize:(list):shapePosition:(list):shapePoints:(integer):shapeRadius:(real):layerPosition:(string):layerPosName:(string)]
*
* Description: Make shape polygon with properties in 4 type of layer position
* ([begin] and [end] do not use layerPosName) [before] and [after]
*)
-- my makeShapeStarWithProperties:"Star23" shapeSize:{200, 200} shapePosition:{200, 200} shapePoints:6 shapeRadius:75 layerPosition:"begin" layerPosName:""
on makeShapeStarWithProperties:_shapeName shapeSize:_Size shapePosition:_Pos shapePoints:_pointsInteger shapeRadius:_inputRadius layerPosition:_layerPosition layerPosName:_layerPosName
tell application "Pixelmator Pro"
tell front document
try
set {theWidth, theHeight} to _Size as list
set theProperties to {name:_shapeName, position:_Pos as list, width:theWidth, height:theHeight, star points:_pointsInteger, star radius:_inputRadius}
if (_layerPosName is "") then -- we do not use layerPosName with 'begin' and 'end'
if (_layerPosition is "begin") then
make new star shape layer at the beginning of layers with properties theProperties
end if
if (_layerPosition is "end") then
make new star shape layer at the end of layers with properties theProperties
end if
end if
if (_layerPosition is "before") then
make new star shape layer at before layer _layerPosName with properties theProperties
end if
if (_layerPosition is "after") then
make new star shape layer at after layer _layerPosName with properties theProperties
end if
end try
end tell
end tell
end makeShapeStarWithProperties:shapeSize:shapePosition:shapePoints:shapeRadius:layerPosition:layerPosName:
--- [Style suite]
(**
* [blendmodeStyleInCurrentSelection:(string)]
*
* Description: Set the style of blend mode of current selection layer.
*)
-- my blendmodeStyleInCurrentSelection:"normal"
on blendmodeStyleInCurrentSelection:_nameString
tell application "Pixelmator Pro"
tell front document to tell current layer
if (_nameString is not "") then
try
if (_nameString = "normal") then
set nameString to normal
end if
if (_nameString = "darken") then
set nameString to darken
end if
if (_nameString = "multiply") then
set nameString to multiply
end if
if (_nameString = "color burn") then
set nameString to color burn
end if
if (_nameString = "linear burn") then
set nameString to linear burn
end if
if (_nameString = "darker color") then
set nameString to darker color
end if
if (_nameString = "lighten") then
set nameString to lighten
end if
if (_nameString = "screen") then
set nameString to screen
end if
if (_nameString = "color dodge") then
set nameString to color dodge
end if
if (_nameString = "linear dodge") then
set nameString to linear dodge
end if
if (_nameString = "lighter color") then
set nameString to lighter color
end if
if (_nameString = "overlay") then
set nameString to overlay
end if
if (_nameString = "soft light") then
set nameString to soft light
end if
if (_nameString = "hard light") then
set nameString to hard light
end if
if (_nameString = "vivid light") then
set nameString to vivid light
end if
if (_nameString = "linear light") then
set nameString to linear light
end if
if (_nameString = "pin light") then
set nameString to pin light
end if
if (_nameString = "hard mix") then
set nameString to hard mix
end if
if (_nameString = "difference") then
set nameString to difference
end if
if (_nameString = "exclusion") then
set nameString to exclusion
end if
if (_nameString = "substract") then
set nameString to substract
end if
if (_nameString = "divide") then
set nameString to divide
end if
if (_nameString = "hue") then
set nameString to hue blend mode
end if
if (_nameString = "saturation") then
set nameString to saturation blend mode
end if
if (_nameString = "color") then
set nameString to color blend mode
end if
if (_nameString = "luminosity") then
set nameString to luminosity
end if
if (_nameString = "pass through") then
set nameString to pass through
end if
if (_nameString = "behind") then
set nameString to behind blend mode
end if
set blend mode to nameString
end try
else
log "error: " & "Check if blend mode string is correct."
error number -128
end if
end tell
end tell
end blendmodeStyleInCurrentSelection:
(**
* [shadowStyleInCurrentSelection:(list)]
*
* Description: Set the style of shadow color of current selected layer
* with default options.
*)
-- my shadowStyleColorInCurrentSelection:{1000, 1000, 45000}
on shadowStyleColorInCurrentSelection:_inputColor
tell application "Pixelmator Pro"
tell front document to tell styles of current layer
set shadow blur to 6
set shadow distance to 3
set shadow angle to 315
set shadow color to _inputColor as list
set shadow opacity to 40
end tell
end tell
end shadowStyleColorInCurrentSelection:
(*
* [strokeStyleColorInCurrentSelection:(list)]
*
* Description: Set the style of stroke color of current selected layer
* with default options.
*)
-- my strokeStyleColorInCurrentSelection:{0, 0, 35000}
on strokeStyleColorInCurrentSelection:_inputColor
tell application "Pixelmator Pro"
tell front document to tell styles of current layer
set stroke width to 2
set stroke position to center -- inside, center or outsie
set stroke type to line -- line, dash or dot
set stroke color to _inputColor as list
set stroke opacity to 75
end tell
end tell
end strokeStyleColorInCurrentSelection:
(**
* [fillStyleColorInCurrentSelection:(list)]
*
* Description: Set the style of fill color of current selected layer
* with default options.
*)
-- my fillStyleColorInCurrentSelection:{1000, 36000, 20000}
on fillStyleColorInCurrentSelection:_inputColor
tell application "Pixelmator Pro"
tell front document to tell styles of current layer
set fill color to _inputColor as list
set fill opacity to 100
end tell
end tell
end fillStyleColorInCurrentSelection:
(*
* [innerShadowStyleColorInCurrentSelection:(list)]
*
* Description: Set the style of inner shadow color of current selected layer
* with default options.
*)
-- my innerShadowStyleColorInCurrentSelection:{1000, 10000, 1000}
on innerShadowStyleColorInCurrentSelection:_inputColor
tell application "Pixelmator Pro"
tell front document to tell styles of current layer
set inner shadow blur to 5
set inner shadow distance to 3
set inner shadow angle to 315
set inner shadow color to _inputColor as list
set inner shadow opacity to 85
end tell
end tell
end innerShadowStyleColorInCurrentSelection:
--~~~~~~~~~ [Supported handlers]
on list_position(this_item, this_list)
repeat with i from 1 to the count of this_list
if item i of this_list contains this_item then return i
end repeat
return 0
end list_position