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

Automatically get document width and height #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ This prototype runs as 3 independent modules:
### 1 - Configure Photoshop

- Go to "Preferences > Plug-ins", enable "Remote Connection" and set a friendly password that you'll need later.
- Make sure that your PS document settings match those in ```server/src/ps.py```, otherwise only an empty layer will be pasted.

<!--
### 2) Setup the local server
Expand Down
24 changes: 12 additions & 12 deletions server/src/ps.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
from photoshop import PhotoshopConnection

# TODO: This offset should be detected by getTopLeft() but the new version
# of Photoshop doesn't seem to support executeActionGet so we put it
# manually here in the meantime.
SCREEN_PIXELS_DENSITY = 2
DOC_OFFSET_X = 74 * SCREEN_PIXELS_DENSITY
DOC_OFFSET_Y = 130 * SCREEN_PIXELS_DENSITY
DOC_WIDTH = 2121
DOC_HEIGHT = 1280

def paste(filename, name, x, y, password='123456'):

Expand All @@ -16,6 +8,8 @@ def paste(filename, name, x, y, password='123456'):
function pasteImage(filename, layerName, x, y) {
var fileRef = new File(filename);
var doc = app.activeDocument;

app.preferences.rulerUnits = Units.PIXELS;

var currentLayer = doc.artLayers.add();
var curr_file = app.open(fileRef);
Expand All @@ -25,7 +19,15 @@ def paste(filename, name, x, y, password='123456'):

doc.paste();
doc.activeLayer.name = layerName;
doc.activeLayer.translate(x, y);

var doc_x = Number(doc.width);
var doc_y = Number(doc.height);

// Some magic numbers in here, but works well
var new_x = x - ((doc_x * 0.5) + (74 * 2));
var new_y = y - ((doc_y * 0.5) + (130 * 2));

doc.activeLayer.translate(new_x, new_y);
try {
doc.activeLayer.move(doc.layers[doc.layers.length - 1], ElementPlacement.PLACEBEFORE);
} catch(e) {
Expand All @@ -45,7 +47,5 @@ def paste(filename, name, x, y, password='123456'):
}
}
"""
x -= DOC_WIDTH * 0.5 + DOC_OFFSET_X
y -= DOC_HEIGHT * 0.5 + DOC_OFFSET_Y
script += f'pasteImage("{filename}", "{name}", {x}, {y})'
conn.execute(script)
conn.execute(script)