Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap Positions With Sizes #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Swap two elements' positions and shift if one is bigger than the other

var onRun = function(context) {
var doc = context.document,
selection = context.selection

if ( [selection count] == 2){

var first_object = selection[0],
second_object = selection[1],
first_position = { left: [[first_object frame] left], top: [[first_object frame] top] },
second_position = { left: [[second_object frame] left], top: [[second_object frame] top] },
first_size = { height: [[first_object frame] height], width: [[first_object frame] width] },
second_size = { height: [[second_object frame] height], width: [[second_object frame] width] }

if (first_position.top < second_position.top || first_position.left < second_position.left) {

// First object is top left

[[first_object frame] setX: (second_position.left - first_size.width + second_size.width)]
[[first_object frame] setY: (second_position.top - first_size.height + second_size.height)]

[[second_object frame] setX: (first_position.left)]
[[second_object frame] setY: (first_position.top)]

} else {

// Second object is top left

[[first_object frame] setX: (second_position.left)]
[[first_object frame] setY: (second_position.top)]

[[second_object frame] setX: (first_position.left - second_size.width + first_size.width)]
[[second_object frame] setY: (first_position.top - second_size.height + first_size.height)]
}
} else {
[doc showMessage:"You need to select exactly two objects"]
}
}
9 changes: 8 additions & 1 deletion Sketch Commands.sketchplugin/Contents/Sketch/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@
"shortcut": "",
"script": "Position/Swap Positions.cocoascript"
},
{
"name": "Swap Positions with sizes",
"identifier": "swappositionswithsizes",
"shortcut": "",
"script": "Position/Swap Positions With Sizes.cocoascript"
},
{
"name": "Make Pill",
"identifier": "makepill",
Expand Down Expand Up @@ -566,7 +572,8 @@
"moveright100px",
"moveup100px",
"setposition…",
"swappositions"
"swappositions",
"swappositionswithsizes"
]
},
{
Expand Down