forked from andyhawkes/ps-paths-to-imagemap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathsToImageMap.jsx
More file actions
167 lines (133 loc) · 4.64 KB
/
PathsToImageMap.jsx
File metadata and controls
167 lines (133 loc) · 4.64 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*****************************************************************
*
* PathsToImageMap 1.0 - by Andy Hawkes - http://www.andyhawkes.co.uk/
*
* v 1.0 - 2010.10.14
*
* Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
*
*****************************************************************/
/*
* -------------------------------------------------------------
* Array search helper function
* -------------------------------------------------------------
*/
function checkArrayForValue(haystack, needle) {
haystack.sort();
for (var i = 0; i < haystack.length; i++) {
if (haystack[i] == needle) {
return true;
}
}
return false;
}
/*
* -------------------------------------------------------------
* PathsToImageMap Core Function
* -------------------------------------------------------------
*/
function doPathExport(el, fileOutput, filePath)
{
// Get the paths within the current document
var paths = el.pathItems;
// Start the image map output
fileOutput.writeln('<map name="imageMap">');
// Loop through all paths
for (var pathIndex = paths.length; pathIndex > 0; pathIndex--)
{
// Get the point coordinates for the current path
var currentPath = paths[pathIndex-1];
// Make an array to hold points
var points = [];
// Make arrays to hold point x and y coordintes so we can check if the current path is rectangular
var xvals = [];
var yvals = [];
// Loop through the sub-path items
for (var u = 0; u < currentPath.subPathItems.length; u++){
for (var t = 0; t < currentPath.subPathItems[u].pathPoints.length; t++){
var point = currentPath.subPathItems[u].pathPoints[t];
// Get the anchor coordinates for the point
var myAnchor = point.anchor;
// Round the values out nicely
var myx = Math.round(myAnchor[0]);
var myy = Math.round(myAnchor[1]);
// Add the coordinates to our arrays
points.push([myx,myy]);
if( checkArrayForValue(xvals,myx) == false ) { xvals.push(myx); }
if( checkArrayForValue(yvals,myy) == false ) { yvals.push(myy); }
}
}
// Output the path coordinate data
fileOutput.writeln('');
if( points.length == 4 && xvals.length == 2 && yvals.length == 2 ) {
var rectPoints = [points[0],points[2]];
fileOutput.writeln('<area shape="rect" coords="' + rectPoints + '" href="***'+ currentPath.name + '***.html" alt="' + currentPath.name +'" title="' + currentPath.name +'"/>');
} else {
fileOutput.writeln('<area shape="poly" coords="' + points + '" href="***'+ currentPath.name + '***.html" alt="' + currentPath.name +'" title="' + currentPath.name +'"/>');
}
}
// Finish the image map output
fileOutput.writeln('');
fileOutput.writeln('</map>');
}
/*
* -------------------------------------------------------------
* PathsToImageMap initialisation function
* -------------------------------------------------------------
*/
function initPathsToImageMap() {
// Establish the correct linefeed type
if ($.os.search(/windows/i) != -1) {
fileLineFeed = "windows";
} else {
fileLineFeed = "macintosh";
}
// Do we have a document open?
if (app.documents.length === 0) {
alert("Please open a file", "PathsToImageMap Error", true);
return;
}
// If we have more than one document open...
if (app.documents.length > 1) {
var runMultiple = confirm("PathsToImageMap has detected multiple open files.\nDo you wish to run PathsToImageMap on all open files?", true, "PathsToImageMap");
if (runMultiple === true) {
docs = app.documents;
} else {
docs = [app.activeDocument];
}
// Or only one document open...
} else {
runMultiple = false;
docs = [app.activeDocument];
}
// Loop through all documents
for (var i = 0; i < docs.length; i++)
{
// Auto set filePath and fileName
filePath = Folder.myDocuments + '/PathsToImageMap-' + docs[i].name + '.txt';
// create outfile
var fileOutput = new File(filePath);
// set linefeed
fileOutput.linefeed = fileLineFeed;
// open for write
fileOutput.open("w", "TEXT", "????");
// Set active document
app.activeDocument = docs[i];
// call to the core with the current document
doPathExport(app.activeDocument, fileOutput, '/');
// close the file
fileOutput.close();
}
// Finish up by displaying a notice (multiple) or opening the output file (single)
if (runMultiple === true) {
alert("PathsToImageMap has parsed " + documents.length + " files;\nFiles were saved in your documents folder", "PathsToImageMap");
} else {
fileOutput.execute();
}
}
/*
* -------------------------------------------------------------
* PathsToImageMap instantiation
* -------------------------------------------------------------
*/
initPathsToImageMap();