123 lines
5.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title> Edit Smart Objects with a Script</title>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0" />
<!--<link rel="stylesheet" href="https://www.photopea.com/tuts/wp-content/themes/simplex/style.css" type="text/css" media="screen" />-->
<link rel="stylesheet" href="https://blog.photopea.com/wp-content/themes/simplex/style.css" type="text/css" media="screen" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i" rel="stylesheet">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-4249565-46"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date()); gtag('config', 'UA-4249565-46');
</script>
</head>
<body>
<div id="page">
<div id="header">
<a href="https://www.photopea.com/tuts/" class="title">Photopea Tutorials</a>
<!--<p>Web-based image editor.</p>-->
<a href="//blog.photopea.com">Blog</a> |
<a href="//www.photopea.com/learn">Learn</a> |
<a class="curr" href="//www.photopea.com/tuts">Tutorials</a> |
<a href="//www.photopea.com/templates">Templates</a> |
<a href="//www.photopea.com/api">API</a> |
<a href="//www.facebook.com/photopea"><img src="//photopea.com/img/facebook.svg" /></a> |
<a href="//www.twitter.com/photopeacom"><img src="//photopea.com/img/twitter.svg" /></a>
</div> <div id="main">
<div id="content">
<div class="post" id="post-325">
<span class="date">October 19, 2019</span>
<h1>Edit Smart Objects with a Script</h1>
<div class="entry">
<p>Let's have a look at how we can edit Smart Objects from a Script in Photopea or Photoshop.</p>
<span id="more-325"></span>
<p>The basic DOM (Document Object Model) allows us working with a document, create and edit layers, modify their properites, etc.
But there are many operations, for which appropriate DOM commands do not exist.</p>
<p>Photopea and Photoshop can <a href="//www.photopea.com/learn/actions">run actions</a>. An action (one step) consists of two parts:</p>
<ul>
<li><b>Name</b> - a string of text, such as "make", "select", etc.</li>
<li><b>Descriptor</b> - a structured object with additional parameters of the action</li>
</ul>
<p>We can create an action (its name and a descriptor) inside a script. Then, it can be executed using <code>executeAction(Name, Descriptor)</code>.</p>
<h2>Open, edit and save a Smart Object</h2>
<p>The action <b>placedLayerEditContents</b> corresponds to opening a Smart Object.</p>
<pre>
// select a layer that you want to work with
var l = app.activeDocument.layers.getByName("arrow");
app.activeDocument.activeLayer = l;
executeAction(stringIDToTypeID("placedLayerEditContents"));
// now, the Smart Object is an active document, we can work with it
app.activeDocument.activeLayer.rotate(90);
// save the smart object and close it
app.activeDocument.save();
app.activeDocument.close();
</pre>
<a target="_blank" href="//www.photopea.com#%7B%22files%22:%5B%22https://www.photopea.com/api/img2/arrows.psd%22%5D%7D">Try it!</a>
<h2>Convert a layer into a Smart Object</h2>
<p>The action <b>newPlacedLayer</b> corresponds to clicking Layer - Convert to Smart Object.</p>
<pre>
// select a layer that you want to work with
var l = app.activeDocument.layers.getByName("ground");
app.activeDocument.activeLayer = l;
executeAction(stringIDToTypeID("newPlacedLayer"));
</pre>
<a target="_blank" href="//www.photopea.com#%7B%22files%22:%5B%22https://www.photopea.com/api/img2/arrows.psd%22%5D%7D">Try it!</a>
</div>
</div>
</div><!--content-->
<div style="width:110px; margin: 0px auto;">
<a href="#" title="Share on Facebook" onclick="smallWindow('http://www.facebook.com/share.php?u=');"><img src="//www.photopea.com/promo/fb.png" /></a>
<a href="#" title="Share on Google Plus" onclick="smallWindow('https://plus.google.com/share?url=');"><img src="//www.photopea.com/promo/gp.png" /></a>
<a href="#" title="Share on Twitter" onclick="smallWindow('http://twitter.com/share?url=');"><img src="//www.photopea.com/promo/tw.png" /></a>
<script type="text/javascript">
function smallWindow(url)
{
var w = 640, h = 300;
window.open(url+"<? echo get_permalink($post); ?>", "Share", "width="+w+",height="+h+",left="+Math.floor((window.innerWidth-w)/2)+",top="+Math.floor((window.innerHeight-h)/2));
}
</script>
</div>
</div>
<div style="max-width:770px; margin: 0px auto;">
<h2 style="margin-top: 3em">Comments</h2>
<p>Do you need help? Ask us at <a href="//www.reddit.com/r/photopea/" target="_blank">our Reddit</a>!</p>
</div>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'photopeablog'; // required: replace example with your forum shortname
(function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
</script>
<!--main-->
<?//php get_sidebar(); ?>
<div id="footer">&copy; 2013-2021 support@photopea.com</div>
</div> <!--page-->
</body>
</html>