Created path from javascript much too large

Participant ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

I have a javascript that takes a named path, gets all the PathPointInfos, writes and writes them to a file. My second script is then intended to re-create this path, by reading all the PathPointInfos and recreating the path. This works when I run the second script on the same image the path was created on, but if I run the second script on a different image with the same dimensions as the first one, the path generated is of the wrong scale (several times larger than the image). Obviously there is something I'm missing. Should I somehow be setting rulerUnits, or similar, to define a "common point of reference" across images? 

 

This is script in its entirety:

 

 

// Settings

var borderThickness = 4;


app.preferences.rulerUnits = Units.PIXELS;


// Initial

var doc_height = app.activeDocument.height;
var doc_width = app.activeDocument.width;
var ratio = doc_height / doc_width;

if (ratio > 1) {
	var feather = doc_width / 1200;
} else {
	var feather = doc_height / 1200;
}


// Frames

var frame = [];

frame[1] = "PointKind.CORNERPOINT 10.0000262260437,36.000047981739 10.0000262260437,36.000047981739 10.0000262260437,36.000047981739 PathPoint;PointKind.SMOOTHPOINT 10.1702928543091,31.0470622777939 11.2540125846863,26.2787758111954 9.52109098434448,33.9033576846123 PathPoint;PointKind.SMOOTHPOINT 18.5761213302612,16.5448471307755 22.271454334259,13.9569126367569 12.6621723175049,20.6861000657082 PathPoint;PointKind.SMOOTHPOINT 34.9999308586121,12.0000874996185 42.0236706733704,11.0532089471817 29.6869039535522,12.7161456346512 PathPoint;PointKind.SMOOTHPOINT 57.1680665016174,11.6439890861511 59.4806671142578,11.6105244159698 49.4983434677124,11.7548944354057 PathPoint;PointKind.SMOOTHPOINT 64.1744256019592,11.5455257296562 67.0235753059387,10.8279659748077 61.8882179260254,12.1215041875839 PathPoint;PointKind.SMOOTHPOINT 69.4655656814575,11.4751641154289 132.33482837677,10.6563522815704 67.6904797554016,11.498331964016 PathPoint;PointKind.SMOOTHPOINT 273.122799396515,10.1792662143707 290.647065639496,10.1417257189751 217.107009887695,10.2987522482872 PathPoint;PointKind.SMOOTHPOINT 324.000012874603,9.99992990493774 337.510621547699,9.97204267978668 306.498277187347,10.0363978147507 PathPoint;PointKind.SMOOTHPOINT 366.173028945923,9.98555725812912 391.523122787476,9.93407315015793 352.676582336426,10.0128009319305 PathPoint;PointKind.SMOOTHPOINT 441.999936103821,9.0000656247139 582.627296447754,8.72698533535004 416.702628135681,9.04919004440308 PathPoint;PointKind.SMOOTHPOINT 859.184074401855,4.11744153499603 871.231162548065,4.09942209720612 720.560646057129,4.32359248399734 PathPoint;PointKind.SMOOTHPOINT 890.000081062317,6.00004374980927 894.864428043365,5.99296468496323 877.970695495605,6.01741963624954 PathPoint;PointKind.SMOOTHPOINT 907.211494445801,6.74098587036133 932.142519950867,6.20512211322784 900.143337249756,6.89286398887634 PathPoint;PointKind.SMOOTHPOINT 984.000134468079,4.99996495246887 1001.5607714653,4.9757245182991 966.890752315521,5.023561835289 PathPoint;PointKind.SMOOTHPOINT 1013.89206647873,3.89992117881775 1101.90864801407,3.78322386741638 996.372306346893,3.92308902740479 PathPoint;PointKind.SMOOTHPOINT 1276.28098726273,3.58694070577621 1517.15515851974,3.33123630285263 1189.39994573593,3.67918306589127 PathPoint;PointKind.SMOOTHPOINT 1983.0175280571,3.07638996839523 1989.786028862,3.07360124588013 1753.33782434464,3.16670167446136 PathPoint;PointKind.SMOOTHPOINT 2003.02852392197,3.0628753900528 2025.79854726791,3.04206722974777 1996.44026756287,3.0688818693161 PathPoint;PointKind.SMOOTHPOINT 2070.99999189377,3.00002187490463 2075.16492605209,2.25500392913818 2047.55147695541,7.19468957185745 PathPoint;PointKind.SMOOTHPOINT 2083.89068841934,2.99787670373917 2103.17233800888,2.99701863527298 2079.53746318817,2.99809122085571 PathPoint;PointKind.SMOOTHPOINT 2141.9998884201,3.00002187490463 2167.73557662964,2.99358636140823 2123.37355613708,3.00474125146866 PathPoint;PointKind.SMOOTHPOINT 2218.99999380112,3.00002187490463 2434.11948680878,2.9607652425766 2193.44583749771,3.00474125146866 PathPoint;PointKind.SMOOTHPOINT 2845.01631259918,4.06424129009247 2864.39967155457,4.07110583782196 2644.5175409317,3.99280709028244 PathPoint;PointKind.SMOOTHPOINT 2904.00002002716,3.00002187490463 2929.07974720001,3.01010417938232 2884.76954698563,2.99229925870895 PathPoint;PointKind.SMOOTHPOINT 2981.00012540817,4.00010067224503 3314.93096351624,4.15626913309097 2956.1859369278,3.98830223083496 PathPoint;PointKind.SMOOTHPOINT 3901.81750059128,4.90343225002289 3934.4456076622,4.94376146793365 3624.30156469345,4.56063389778137 PathPoint;PointKind.SMOOTHPOINT 3999.99986886978,4.00010067224503 4023.51951599121,4.03099113702774 3968.27105283737,3.95826983451843 PathPoint;PointKind.SMOOTHPOINT 4068.16091537476,5.12052357196808 4108.83275270462,5.17651253938675 4045.14756202698,5.08877503871918 PathPoint;PointKind.SMOOTHPOINT 4187.75278329849,5.29106467962265 4356.6917181015,5.54097712039948 4148.70604276657,5.23335957527161 PathPoint;PointKind.SMOOTHPOINT 4645.84983587265,6.06161016225815 4664.25601243973,6.09657645225525 4510.15377044678,5.80483317375183 PathPoint;PointKind.SMOOTHPOINT 4700.08152723312,6.16586548089981 4721.85215950012,6.2081253528595 4682.33517408371,6.1313282251358 PathPoint;PointKind.SMOOTHPOINT 4765.00010490417,4.99996495246887 5020.91181278229,5.51437699794769 4744.24109458923,4.95834863185883 PathPoint;PointKind.SMOOTHPOINT 5314.93878364563,13.3356710672379 5319.09116506577,13.3802906274796 5222.15183973312,12.3379519581795 PathPoint;PointKind.SMOOTHPOINT 5326.75187587738,13.4645958542824 5331.56279325485,12.561049759388 5323.09420108795,14.1514796614647 PathPoint;PointKind.SMOOTHPOINT 5337.79925107956,13.5883722305298 5360.12091636658,13.842360496521 5334.36237573624,13.5493301153183 PathPoint;PointKind.CORNERPOINT 5371.99999094009,14.0000305771828 5371.99999094009,14.0000305771828 5371.99999094009,14.0000305771828 PathPoint;PointKind.SMOOTHPOINT 5386.99922561646,20.3018999099731 5389.626288414,22.7894403934479 5380.71352243423,14.3492644429207 PathPoint;PointKind.SMOOTHPOINT 5392.39979982376,32.2970535159111 5392.75417327881,34.3040756583214 5391.43613576889,26.8348041772842 PathPoint;PointKind.SMOOTHPOINT 5393.00007820129,39.0000698566437 5393.0370926857,41.6336964964867 5392.96499490738,36.5305488109589 PathPoint;PointKind.SMOOTHPOINT 5393.10532808304,46.980750143528 5393.18708181381,53.5966725349426 5393.07217597961,44.2945668101311 PathPoint;PointKind.SMOOTHPOINT 5393.31647157669,67.2601257562637 5393.33803653717,69.7579630613327 5393.25692653656,60.3659746646881 PathPoint;PointKind.SMOOTHPOINT 5393.37730407715,74.8016895055771 5393.93606185913,78.1378597021103 5392.88967847824,71.8896196484566 PathPoint;PointKind.SMOOTHPOINT 5393.4471487999,85.0268623828888 5393.92318725586,162.425281584263 5393.4259057045,81.5937304496765 PathPoint;PointKind.SMOOTHPOINT 5394.00011301041,317.000094771385 5394.15299892426,332.566100299358 5393.35992336273,251.922466158867 PathPoint;PointKind.SMOOTHPOINT 5394.00011301041,368.000037610531 5394.14398670197,383.217667341232 5393.85269880295,352.452266037464 PathPoint;PointKind.SMOOTHPOINT 5392.68432855606,413.85886323452 5393.55690479279,509.625954091549 5392.54592657089,398.661398112774 PathPoint;PointKind.SMOOTHPOINT 5393.98047924042,697.620174348354 5394.19838190079,728.817184090614 5393.31904649734,602.973720908165 PathPoint;PointKind.SMOOTHPOINT 5396.99989557266,785.000074982643 5397.1334695816,806.094615638256 5396.80355787277,753.965454697609 PathPoint;PointKind.SMOOTHPOINT 5394.98823881149,854.133364200592 5396.33460044861,1082.42269414663 5394.864320755,833.121841669083 PathPoint;PointKind.SMOOTHPOINT 5393.48255395889,1520.67451322079 5393.6309337616,1607.93342620134 5393.11788082123,1306.62804722786 PathPoint;PointKind.SMOOTHPOINT 5392.00004339218,1768.9999563694 5392.00583696365,1795.1141975522 5391.98105335236,1685.06112480164 PathPoint;PointKind.SMOOTHPOINT 5393.72974634171,1855.42139452696 5393.6573266983,2203.21606492996 5393.73489618301,1829.66346627474 PathPoint;PointKind.SMOOTHPOINT 5392.227602005,2779.6033924818 5392.07181930542,2809.61476612091 5393.58201026917,2518.76473689079 PathPoint;PointKind.SMOOTHPOINT 5389.99997377396,2865.99994665384 5389.81490135193,2898.61040967703 5390.16219377518,2837.44192647934 PathPoint;PointKind.SMOOTHPOINT 5389.99997377396,2966.99996805191 5389.74698781967,3007.72089421749 5390.19019603729,2936.3714286685 PathPoint;PointKind.SMOOTHPOINT 5390.42258262634,3079.03110313416 5388.99350166321,3286.38785284758 5390.6778216362,3042.00437623262 PathPoint;PointKind.SMOOTHPOINT 5389.75954055786,3509.34943568707 5389.67553377151,3514.73124110699 5390.87898731232,3437.71852368116 PathPoint;PointKind.SMOOTHPOINT 5390.52493572235,3530.11748129129 5390.29222726822,3533.23613113165 5390.85162878036,3525.73725628853 PathPoint;PointKind.SMOOTHPOINT 5390.3826713562,3538.80649709702 5390.14095067978,3553.39687877893 5390.42676687241,3536.14197999239 PathPoint;PointKind.CORNERPOINT 5389.99997377396,3561.0000089407 5389.99997377396,3561.0000089407 5389.99997377396,3561.0000089407 PathPoint;PointKind.SMOOTHPOINT 5390.03731012344,3563.03835058212 5389.91210460663,3569.45970594883 5390.06209373474,3561.76347535849 PathPoint;PointKind.SMOOTHPOINT 5364.00003433228,3589.00007009506 5320.25084495544,3589.27057617903 5387.58147954941,3588.85419845581 PathPoint;PointKind.SMOOTHPOINT 5214.01476860046,3589.50204014778 5210.92711687088,3589.50869017839 5264.81215953827,3589.39478158951 PathPoint;PointKind.SMOOTHPOINT 5204.74923849106,3589.52156120539 5199.85399246216,3589.99392789602 5208.11787843704,3589.1963532567 PathPoint;PointKind.SMOOTHPOINT 5190.00202417374,3589.55202263594 5140.99227190018,3589.65370374918 5194.91368532181,3589.54194033146 PathPoint;PointKind.SMOOTHPOINT 5050.46063661575,3589.98470366001 5039.39008712769,3590.04412490129 5092.57829189301,3589.7590316534 PathPoint;PointKind.SMOOTHPOINT 5035.03428697586,3588.11840474606 5033.74103307724,3588.13363546133 5041.59421920776,3588.04139310122 PathPoint;PointKind.SMOOTHPOINT 5029.99999523163,3587.99999129772 5008.48073959351,3588.11282730103 5032.11851119995,3587.98883640766 PathPoint;PointKind.SMOOTHPOINT 4889.9999499321,3589.00007009506 4853.59025001526,3589.18434029818 4911.49506568909,3588.89109539986 PathPoint;PointKind.SMOOTHPOINT 4836.00000143051,3589.00007009506 4830.728495121,3589.13199812174 4858.20000171661,3588.44425624609 PathPoint;PointKind.SMOOTHPOINT 4811.39470338821,3591.18557047844 4562.36511468887,3592.35340166092 4820.01650333405,3591.14502674341 PathPoint;PointKind.SMOOTHPOINT 4072.77388572693,3591.27223539352 4032.09528923035,3591.1746301055 4315.7005906105,3591.85464936495 PathPoint;PointKind.SMOOTHPOINT 3951.15265846252,3591.26043695211 3922.86232709885,3591.33165663481 3991.54962301254,3591.15897035599 PathPoint;PointKind.SMOOTHPOINT 3861.99989318848,3589.99993437529 3813.60039710999,3590.11212682724 3890.16115665436,3589.93472117186 PathPoint;PointKind.SMOOTHPOINT 3721.87947034836,3591.77635061741 3700.69495439529,3591.81861048937 3769.87019777298,3591.68110501766 PathPoint;PointKind.SMOOTHPOINT 3658.44919681549,3591.90398830175 3642.55195856094,3588.96446025372 3679.19919490814,3595.74127048254 PathPoint;PointKind.SMOOTHPOINT 3615.69135189056,3591.99172580242 3601.41277313232,3592.02111464739 3629.92422580719,3591.96233695745 PathPoint;PointKind.SMOOTHPOINT 3572.91548252106,3592.08032137156 3293.95179748535,3592.66209179163 3587.15382814407,3592.05071800947 PathPoint;PointKind.SMOOTHPOINT 2763.07461261749,3593.6988530159 2740.99434614182,3593.73424834013 3022.7859377861,3593.28354787827 PathPoint;PointKind.SMOOTHPOINT 2697.08390235901,3593.80031961203 2683.98978710175,3595.97530865669 2722.81701564789,3589.52585154772 PathPoint;PointKind.SMOOTHPOINT 2657.35802650452,3593.85695213079 2395.48248052597,3594.21905702353 2670.5683350563,3593.83871817589 PathPoint;PointKind.SMOOTHPOINT 1912.99899816513,3593.93310570717 1873.4327673912,3593.86531829834 2146.06568813324,3594.3314639926 PathPoint;PointKind.SMOOTHPOINT 1795.71586847305,3593.71923214197 1753.88853549957,3593.63835918903 1834.33227539062,3593.79409861565 PathPoint;PointKind.SMOOTHPOINT 1671.96035385132,3593.46524387598 1636.11005544662,3593.38737416267 1712.62897253036,3593.553624928 PathPoint;PointKind.SMOOTHPOINT 1565.80313444138,3593.22391211987 1364.12440538406,3592.74489539862 1600.71905851364,3593.30671572685 PathPoint;PointKind.SMOOTHPOINT 1011.36671304703,3591.15146225691 1002.70210504532,3591.10920238495 1178.29366922379,3591.96448212862 PathPoint;PointKind.SMOOTHPOINT 984.000134468079,3589.99993437529 969.163119792938,3592.8937702775 992.40403175354,3588.36080908775 PathPoint;PointKind.SMOOTHPOINT 940.585041046143,3590.79965418577 782.318186759949,3589.9988617897 955.403065681458,3590.87452065945 PathPoint;PointKind.SMOOTHPOINT 524.440848827362,3588.6673540473 491.162896156311,3588.51633399725 642.643225193024,3589.20364683867 PathPoint;PointKind.SMOOTHPOINT 422.999918460846,3586.00004822016 400.038707256317,3585.89021545649 452.809774875641,3586.14248758554 PathPoint;PointKind.SMOOTHPOINT 364.066421985626,3587.90667635202 252.771592140198,3587.35472381115 384.902358055115,3588.0100736022 PathPoint;PointKind.SMOOTHPOINT 75.6028890609741,3586.21027499437 65.5858039855957,3585.98159974813 121.219539642334,3587.25154107809 PathPoint;PointKind.SMOOTHPOINT 59.057092666626,3586.15342795849 60.7632994651794,3586.29093343019 58.7284684181213,3586.12704235315 PathPoint;PointKind.SMOOTHPOINT 56.0186862945557,3586.13347786665 43.0758476257324,3586.0485290885 56.9981217384338,3586.14012789726 PathPoint;PointKind.CORNERPOINT 35.9999656677246,3586.00004822016 35.9999656677246,3586.00004822016 35.9999656677246,3586.00004822016 PathPoint;PointKind.SMOOTHPOINT 13.9998435974121,3556.99990826845 13.9126181602478,3549.8753657937 14.3580794334412,3586.20126527548 PathPoint;PointKind.SMOOTHPOINT 13.7613415718079,3535.51945132017 14.1189336776733,3531.83254563808 13.0670785903931,3542.68067622185 PathPoint;PointKind.SMOOTHPOINT 13.6625289916992,3525.08297908306 13.621973991394,3520.56224536896 13.6937499046326,3528.56931126118 PathPoint;PointKind.SMOOTHPOINT 13.5489106178284,3511.4851680994 13.0912184715271,3452.45413345098 13.5839939117432,3516.02928417921 PathPoint;PointKind.SMOOTHPOINT 13.1401419639587,3329.15613037348 13.1504416465759,3319.25187510252 13.0744814872742,3390.84760630131 PathPoint;PointKind.SMOOTHPOINT 13.1765127182007,3299.44658231735 10.4689836502075,3288.97879058123 16.1711454391479,3311.02471464872 PathPoint;PointKind.SMOOTHPOINT 13.2202863693237,3267.05213803053 13.3107304573059,3197.85320657492 13.2061243057251,3277.831623137 PathPoint;PointKind.SMOOTHPOINT 12.8353357315063,3065.86640220881 12.559175491333,3031.85213917494 13.35289478302,3129.60673111677 PathPoint;PointKind.SMOOTHPOINT 12.0000958442688,2966.99996805191 11.8336915969849,2944.12986922264 12.2460007667542,3000.78598487377 PathPoint;PointKind.SMOOTHPOINT 11.0000610351562,2892.00006473064 10.9228134155273,2880.52790385485 11.1529469490051,2914.75754207373 PathPoint;PointKind.SMOOTHPOINT 11.3473534584045,2861.34578329325 9.32056903839111,2546.80040788651 11.4210605621338,2872.78812628984 PathPoint;PointKind.SMOOTHPOINT 4.62487936019897,1961.59406715631 4.63453531265259,1919.75743746758 4.55760955810547,2244.68672841787 PathPoint;PointKind.SMOOTHPOINT 2.0000696182251,1839.99997353554 2.02066898345947,1821.02357488871 1.95533037185669,1880.93434423208 PathPoint;PointKind.SMOOTHPOINT 4.78034019470215,1780.79711067677 5.59111833572388,1233.29880321026 4.75265979766846,1799.57079064846 PathPoint;PointKind.SMOOTHPOINT 8.61954689025879,467.823647141457 8.69357585906982,459.650330483913 5.86534738540649,772.267197012901 PathPoint;PointKind.SMOOTHPOINT 8.84678363800049,443.643492281437 10.3151321411133,433.869234383106 7.66650438308716,451.50189781189 PathPoint;PointKind.SMOOTHPOINT 9.13485288619995,415.474820673466 9.17701721191406,411.537144482136 9.03604030609131,424.697555065155 PathPoint;PointKind.SMOOTHPOINT 9.26295518875122,403.753390908241 9.65723991394043,368.337473034859 9.21950340270996,407.629929721355 PathPoint;PointKind.SMOOTHPOINT 8.99999141693115,312.99999409914 9.22336578369141,294.436326384544 8.63692760467529,343.17826205492 PathPoint;PointKind.SMOOTHPOINT 8.99999141693115,252.999985635281 8.40936899185181,178.287535250187 9.13034677505493,269.498926103115 PathPoint;PointKind.SMOOTHPOINT 8.99999141693115,66.9999164938927 8.91212224960327,61.3405259251595 9.61217880249023,106.363592863083 PathPoint;PointKind.SMOOTHPOINT 10.0000262260437,52.0000216364861 10.8272194862366,38.1613079309464 9.63149070739746,58.1648145318031 PathPoint";



function setOrientation(){
	
	
	
}


// Build the path

function createPath(thisFrame) {
	
	var thisPathPointInfo = thisFrame.split(";");
	var thisPathPointInfoProperties = [];
	
	var p = [];
	var pth = [];
	
	for (var i = 0; i < thisPathPointInfo.length; i++ ) {
		
		thisPathPointInfoProperties = thisPathPointInfo[i].split(" "); // Splitta thisPathPointInfo i dess properties
		
		p[i] = new PathPointInfo();
		p[i].kind = thisPathPointInfoProperties[0];
	
		var anchorString = thisPathPointInfoProperties[1].split(",");
		p[i].anchor = [anchorString[0]*1, anchorString[1]*1];
		
		var leftDirectionString = thisPathPointInfoProperties[2].split(",");
		p[i].leftDirection = [leftDirectionString[0]*1, leftDirectionString[1]*1];
		
		var rightDirectionString = thisPathPointInfoProperties[3].split(",");
		p[i].rightDirection = [rightDirectionString[0]*1, rightDirectionString[1]*1];

		p[i].typename = thisPathPointInfoProperties[4];
	
	}

	pth[0] = new SubPathInfo();
	pth[0].operation = ShapeOperation.SHAPEXOR;
	pth[0].closed = true;
	pth[0].entireSubPath = p;
	
	app.activeDocument.pathItems.add("Frame", pth);
	
}

function biggerCanvas() {
	
	if ( ratio > 1 ) {
			
			var new_doc_height = doc_height * borderThickness / ratio / 100 + doc_height;
			var new_doc_width = doc_width * borderThickness / 100 + doc_width;
	
			var offset = borderThickness / 100 * doc_width / generateRandomInteger(10);
			
			// Offset
			app.activeDocument.resizeCanvas(UnitValue(offset + doc_width,"px"), UnitValue(doc_height,"px"), AnchorPosition.MIDDLERIGHT);
			
			// Enlarging canvas
			app.activeDocument.resizeCanvas(UnitValue(new_doc_width,"px"), UnitValue(new_doc_height,"px"), AnchorPosition.MIDDLECENTER);
			
		} else {
			
			var new_doc_height = doc_height * borderThickness / 100 + doc_height;
			var new_doc_width = doc_width * borderThickness * ratio / 100 + doc_width;
			
			var offset = borderThickness / 100 * doc_height / generateRandomInteger(10);
			
			// Offset
			app.activeDocument.resizeCanvas(UnitValue(doc_width,"px"), UnitValue(offset + doc_height,"px"), AnchorPosition.TOPCENTER);
			
			// Enlarging canvas
			app.activeDocument.resizeCanvas(UnitValue(new_doc_width,"px"), UnitValue(new_doc_height,"px"), AnchorPosition.MIDDLECENTER);
		}

}


// Generate a number between 0 and max, including max
function generateRandomInteger(max) {
    return Math.floor(Math.random() * max) + 1;
}

if (doc_height > doc_width) {
	var orientation = "portrait";
	
	if (doc_height/doc_width > 1.4) {
		var aspect_ratio = "35mm";
	}
	else {
		var aspect_ratio = "645";
	}
}
else {
	var orientation = "landscape"
	
	if (doc_width/doc_height > 1.4) {
		var aspect_ratio = "35mm";
	}
	else {
		var aspect_ratio = "645";
	}
}




// LET'S GET THIS SHOW GOING!!!!

var useFrame = frame[1]; // this sets the used frame. Change for logic.

// Drawing path

if (ratio > 1 ){
	// For portrait we need to rotate before placing path.
	app.activeDocument.rotateCanvas(-90);
	createPath(useFrame);
	app.activeDocument.rotateCanvas(90);
	
} else {
	createPath(useFrame);
}

//biggerCanvas(); // Enlarge canvas

//app.activeDocument.pathItems.getByName('Frame').makeSelection(feather, true); // Make selection from path

//app.activeDocument.pathItems.getByName('Frame').remove(); // Trash path

//app.activeDocument.selection.invert(); // Invert selection

var myColor = new SolidColor();  
myColor.rgb.red = 0;  
myColor.rgb.green = 0;  
myColor.rgb.blue = 0;  

//app.activeDocument.selection.fill(myColor); // Fill with black

//app.activeDocument.selection.deselect(); // Unnecessary?

//app.activeDocument.save();

//app.activeDocument.close();

 

TOPICS
Actions and scripting

Views

112

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

Found a "solution", whereby you resample the image to 72dpi first, like this:

 

app.activeDocument.resizeImage(null, null, 72, ResampleMethod.NONE);

 

Does seem a little hacky, though...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

Have you tried using Units.POINTS? 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

Tried it now in the script that builds the path, but it didn't make a difference. Unless I need to set Unit.POINTS in both the script that exports the path and in the script that rebuilds it?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

What were the RulerUnits and the image’s resolution when you created the Array? 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

Ruler units were both set to PIXELS. Resolution differed, and it won't be something I'll have control over once my script is used by others. Maybe always setting resolution to 72dpi (but keep image dimensions) is an acceptable way to handle this. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

The best solution would be if you could resize a path to fit the canvas using JavaScript. Is this possible?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

Resolution differed

Well, that seems to be it then. 

 


The best solution would be if you could resize a path to fit the canvas using JavaScript. Is this possible?


Yes, but considering that paths can have curves and therefore do not necessarily have PathPoints at the extremes in either direction that would be cumbersome. 

A possible work-araound would be creating a Shape Layer and scaling that based on its bounding box. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

Would it be possible to show me just how to draw that path on a shape layer instead, resize the layer, and then either convert the layer shape back to a path, or make a selection directly from that shape?

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

With the path is selected try this: 

 

// 2022, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theShapeLayer = createSolidColorLayer (0,255,128);
scaleToCanvasSize ();
loadVectorMask ();
theShapeLayer.remove();
};
////// make solid color layer //////
function createSolidColorLayer (theRed, theGreen, theBlue) {
// create solid color layer;
// =======================================================
var desc8 = new ActionDescriptor();
    var ref6 = new ActionReference();
    var idcontentLayer = stringIDToTypeID( "contentLayer" );
    ref6.putClass( idcontentLayer );
desc8.putReference( charIDToTypeID( "null" ), ref6 );
    var desc9 = new ActionDescriptor();
        var desc10 = new ActionDescriptor();
            var desc11 = new ActionDescriptor();
            desc11.putDouble( charIDToTypeID( "Rd  " ), theRed );
            desc11.putDouble( charIDToTypeID( "Grn " ), theGreen );
            desc11.putDouble( charIDToTypeID( "Bl  " ), theBlue );
        var idRGBC = charIDToTypeID( "RGBC" );
        desc10.putObject( charIDToTypeID( "Clr " ), idRGBC, desc11 );
    desc9.putObject( charIDToTypeID( "Type" ), stringIDToTypeID( "solidColorLayer" ), desc10 );
desc8.putObject( charIDToTypeID( "Usng" ), idcontentLayer, desc9 );
executeAction( charIDToTypeID( "Mk  " ), desc8, DialogModes.NO );
return activeDocument.activeLayer
};
////// scale active layer to canvas dimensions //////
function scaleToCanvasSize () {
// scale smart object:
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var theBounds = layerDesc.getObjectValue(stringIDToTypeID('bounds'));
var theX = theBounds.getInteger(stringIDToTypeID('left'));
var theY = theBounds.getInteger(stringIDToTypeID('top'));
var theX2 = theBounds.getInteger(stringIDToTypeID('right'));
var theY2 = theBounds.getInteger(stringIDToTypeID('bottom'));
// determine the scale;
var theSOProp = activeDocument.width/activeDocument.height;
var theNewProp = (theX2 - theX)/(theY2 - theY);
if (theNewProp >= theSOProp) {var theScale = activeDocument.width / (theX2 - theX) * 100}
else {var theScale = activeDocument.height / (theY2 - theY) * 100};
// transform;
var desc23 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc23.putReference( charIDToTypeID( "null" ), ref2 );
var idOfst = charIDToTypeID( "Ofst" );
  var desc24 = new ActionDescriptor();
  var idPxl = charIDToTypeID( "#Pxl" );
  desc24.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, activeDocument.width/2 - (theX+(theX2-theX)/2) );
  desc24.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, activeDocument.height/2 - (theY+(theY2-theY)/2) );
desc23.putObject( idOfst, idOfst, desc24 );
var idPrc = charIDToTypeID( "#Prc" );
desc23.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theScale );
desc23.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theScale );
desc23.putEnumerated( charIDToTypeID( "Intr" ), charIDToTypeID( "Intp" ), stringIDToTypeID( "bicubicAutomatic" ) );
desc23.putEnumerated( stringIDToTypeID( "freeTransformCenterState" ), stringIDToTypeID( "quadCenterState" ), stringIDToTypeID( "QCSAverage" ) );
//            desc23.putBoolean( charIDToTypeID( "Cpy " ), true );
executeAction( charIDToTypeID( "Trnf" ), desc23, DialogModes.NO );
app.preferences.rulerUnits = originalRulerUnits;
};
////// load vector mask as selection //////
function loadVectorMask () {
    var desc5 = new ActionDescriptor();
        var ref1 = new ActionReference();
        ref1.putProperty( stringIDToTypeID( "channel" ), stringIDToTypeID( "selection" ) );
    desc5.putReference( stringIDToTypeID( "null" ), ref1 );
        var ref2 = new ActionReference();
        var idpath = stringIDToTypeID( "path" );
        ref2.putEnumerated( idpath, idpath, stringIDToTypeID( "vectorMask" ) );
        ref2.putEnumerated( stringIDToTypeID( "layer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ));
    desc5.putReference( stringIDToTypeID( "to" ), ref2 );
    desc5.putInteger( stringIDToTypeID( "version" ), 1 );
    desc5.putBoolean( stringIDToTypeID( "vectorMaskParams" ), true );
executeAction( stringIDToTypeID( "set" ), desc5, DialogModes.NO );
};

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

This works, which is amazing, BUT the edges of the mask seems blured? Is this unavoidable from the scaling?

 

Thank you for all your effort here!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

*the edges of the selection, I mean. I looks like loading the vector mask blurres the edges.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 15, 2022 Aug 15, 2022

Copy link to clipboard

Copied


@hertze wrote:

This works, which is amazing, BUT the edges of the mask seems blured? Is this unavoidable from the scaling?

 

Thank you for all your effort here!


Scaling a Path (including Vector Masks) does not blur it. 

Could you check if a Feather is set for the Shape Layer in the Properties Panel? 

Could you post screenshots with the pertinent Panels visible to show what is going on? 

 

I'm filling it with black color, to create a fake scanned negative border.

Then wouldn’t a Shape Layer work just as well anyways? 

The ShapeOperation of the subPathItems might need to be inverted, though. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 21, 2022 Aug 21, 2022

Copy link to clipboard

Copied

LATEST

Shapes might be the answer! I still have much to learn about how to use them with JavaScript before I can make use of them, though. The JavaScript reference is rather light on the subject. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

Might working with Shape Layers work out in your workflow? 

What are you doing with the Selection? 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 13, 2022 Aug 13, 2022

Copy link to clipboard

Copied

I'm filling it with black color, to create a fake scanned negative border.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines