-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathDynamic Script.jsx
112 lines (83 loc) · 2.7 KB
/
Dynamic Script.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
Super Export.jsx
by: Josh Wright
company: Bendy Tree, LLC (http://www.bendytree.com)
created: July 30, 2011
repo: https://github.com/bendytree/Photoshop-Scripts
This is a Photoshop script that lets you makes it easy to dynamically run
any arbitrary Javascript in Photoshop.
For installation instructions or for other scripts by Bendy Tree, see:
https://github.com/bendytree/Photoshop-Scripts
HOW IT WORKS
To use it, create a text layer named 'script' and put your code as the text
on that layer. For example, you might write:
alert('The path is: '+app.activeDocument.path);
When you run this script, an alert box should pop up telling you the path
of the active document.
**/
var doc = app.activeDocument;
var stringIDToTypeID = function(s){
return app.stringIDToTypeID(s);
};
var charIDToTypeID = function(s){
return app.charIDToTypeID(s);
};
/********************************************************************************/
/******************************** EXTENSIONS **********************************/
String.prototype.trim = function(){
return this.replace(/^ */, "").replace(/ *$/, "");
}
Object.prototype.keys = function(){
var keys = [];
for(i in this) if (this.hasOwnProperty(i))
{
keys.push(i);
}
return keys;
};
Array.prototype.indexOf = function(el) {
for (var i = 0; i < this.length; i += 1) {
if (this[i] == el) return i;
}
return -1;
};
Array.prototype.lastIndexOf = function(el) {
for (var i = this.length-1; i >= 0; i -= 1) {
if (this[i] == el) return i;
}
return -1;
};
Array.prototype.distinct = function() {
var derivedArray = [];
for (var i = 0; i < this.length; i += 1) {
if (derivedArray.indexOf(this[i]) == -1) {
derivedArray.push(this[i])
}
}
return derivedArray;
};
function eachLayer(callback, d){
d = d || doc;
for(var i=0; i<d.layers.length; i++){
var result = callback(d.layers[i]);
if(result == false)
return;
if(d.layers[i].layers)
eachLayer(callback, d.layers[i]);
}
}
var foundScript = false;
eachLayer(function(l){
if(l.kind == LayerKind.TEXT && l.name.match(/^script$/i)){
foundScript = true;
var script = l.textItem.contents;
try{
eval(script.replace(/”/g, "\"").replace(/’/g, "'"));
}catch(e){
alert("Error: '"+e + "' on:\n\n"+script);
}
return false;
}
});
if(!foundScript)
alert("No Script Found\n\nCreate a text layer named 'script' to run a dynamic script.");