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

Some changes to export script #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
65 changes: 40 additions & 25 deletions Export Page to PNG.sketchplugin
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
// (cmd control option e)
#import 'Export-Page/sandbox.js'

// Get the frame for the current page, add padding, slice it
var frame = [[doc currentPage] contentBounds];
[frame expandBy:100];
var slice = frame;

// Get the path and name of the file
var fileDisplayName = [doc displayName];
var currentFilePath = [[doc fileURL] path];
var fileFolder = currentFilePath.split(fileDisplayName)[0];
var fileName = fileDisplayName.substring(0, fileDisplayName.indexOf("."));

// Save the page slice to the same directory as a PNG
var fullPath = fileFolder + fileName + ".png";
var savedPage = [doc currentPage];

function in_sandbox(){
var environ = [[NSProcessInfo processInfo] environment];
return (nil != [environ objectForKey:@"APP_SANDBOX_CONTAINER_ID"]);
}

if(in_sandbox()) {
var sandboxAccess = AppSandboxFileAccess.init({
message: "Please authorize Sketch to write to this folder. You will only need to do this once per folder.",
prompt: "Authorize",
title: "Sketch Authorization"
});
sandboxAccess.accessFilePath_withBlock_persistPermission(fullPath, function(){
[doc saveArtboardOrSlice:slice toFile:fullPath];
}, true);
} else {
[doc saveArtboardOrSlice:slice toFile:fullPath];
function export_current_page() {
// Get the frame for the current page, add padding, slice it
var frame = [[doc currentPage] contentBounds];
[frame expandBy:100];
var slice = frame;

// Get the path and name of the file
var fileDisplayName = [doc displayName];
var currentFilePath = [[doc fileURL] path];
var fileFolder = currentFilePath.split(fileDisplayName)[0];
var fileName = [[doc currentPage] name];

// Save the page slice to the same directory as a PNG
var fullPath = fileFolder + fileName + ".png";

if(in_sandbox()) {
var sandboxAccess = AppSandboxFileAccess.init({
message: "Please authorize Sketch to write to this folder. You will only need to do this once per folder.",
prompt: "Authorize",
title: "Sketch Authorization"
});
sandboxAccess.accessFilePath_withBlock_persistPermission(fullPath, function(){
[doc saveArtboardOrSlice:slice toFile:fullPath];
}, true);
} else {
[doc saveArtboardOrSlice:slice toFile:fullPath];
}
[doc showMessage:"Saved as ./" + fileName + ".png"];
}

var pages = [doc pages];

for (var i=0; i < [pages count]; i++) {

var page = [pages objectAtIndex:i];

[doc setCurrentPage:page];
export_current_page();
}
[doc showMessage:"Saved as ./" + fileName + ".png"];

[doc setCurrentPage:savedPage];