Skip to content

Commit 3dd89ae

Browse files
committed
chore: Update .gitignore and dependencies for better project management
Update .gitignore to include new example files directories and update dependencies with Poetry 1.8.5. Signed-off-by: longhao <[email protected]>
1 parent 7f8470c commit 3dd89ae

17 files changed

+245
-60
lines changed

.github/workflows/code-check.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ name: Code Quality Check
22

33
on: [pull_request]
44

5+
# Add concurrency configuration
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.ref }}
8+
cancel-in-progress: true
9+
510
jobs:
611
lint:
712
runs-on: ubuntu-latest
@@ -27,9 +32,11 @@ jobs:
2732
2833
test:
2934
runs-on: windows-latest
35+
needs: lint # Add dependency on lint job
3036
strategy:
3137
matrix:
3238
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
39+
fail-fast: false # Continue with other versions if one fails
3340
steps:
3441
- uses: actions/checkout@v4
3542
with:

.github/workflows/python-publish.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ on:
55
tags:
66
- "v*"
77

8+
# Add concurrency configuration
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
813
jobs:
914
deploy:
1015
runs-on: ubuntu-latest

.gitignore

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ venv_python
3232
# Docs
3333
docs_src/_build/
3434
/run_pycharm.bat
35-
36-
37-
# windsurf rules
38-
.windsurfrules
35+
36+
37+
# windsurf rules
38+
.windsurfrules
39+
/examples/files/bg/
40+
/examples/files/blue/
41+
/examples/files/green/
42+
/examples/files/red/

docs/gen_examples.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Import built-in modules
44
from __future__ import annotations
55

6-
import os
76
from pathlib import Path
87

98
import mkdocs_gen_files

examples/_psd_files.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Import built-in modules
22
from __future__ import annotations
3-
from pathlib import Path
3+
44
import os
5+
from pathlib import Path
56

67

78
def get_psd_files() -> dict[str, str]:

examples/active_layer.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
1-
# Set the active layer to the last art layer of the active document, or the
2-
# first if the last is already active.
3-
4-
# Import local modules
5-
from __future__ import annotations
6-
7-
from photoshop import Session
8-
9-
with Session() as ps:
10-
if len(ps.app.documents) < 1:
11-
docRef = ps.app.documents.add()
12-
else:
13-
docRef = ps.app.activeDocument
14-
15-
if len(docRef.layers) < 2:
16-
docRef.artLayers.add()
17-
18-
ps.echo(docRef.activeLayer.name)
19-
new_layer = docRef.artLayers.add()
20-
ps.echo(new_layer.name)
21-
new_layer.name = "test"
1+
"""Demonstrate how to work with active layers in Photoshop.
2+
3+
This example shows how to:
4+
1. Create a new document if none exists
5+
2. Add new art layers to the document
6+
3. Get and set the active layer
7+
4. Rename layers
8+
9+
Example:
10+
```python
11+
with Session() as ps:
12+
docRef = ps.app.documents.add() # Create new document
13+
new_layer = docRef.artLayers.add() # Add new layer
14+
new_layer.name = "test" # Rename layer
15+
```
16+
17+
Note:
18+
The script will create a new document if none exists,
19+
and will add a new layer if the document has less than 2 layers.
20+
"""
21+
22+
# Import built-in modules
23+
from __future__ import annotations
24+
25+
# Import local modules
26+
from photoshop import Session
27+
28+
# Create a new Photoshop session
29+
with Session() as ps:
30+
# Create a new document if none exists
31+
if len(ps.app.documents) < 1:
32+
docRef = ps.app.documents.add()
33+
else:
34+
docRef = ps.app.activeDocument
35+
36+
# Add a new layer if document has less than 2 layers
37+
if len(docRef.layers) < 2:
38+
docRef.artLayers.add()
39+
40+
# Print the name of the current active layer
41+
ps.echo(docRef.activeLayer.name)
42+
43+
# Create a new art layer and make it active
44+
new_layer = docRef.artLayers.add()
45+
ps.echo(new_layer.name)
46+
47+
# Rename the new layer
48+
new_layer.name = "test"

examples/add_metadata.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
"""Add metadata to current active document."""
1+
"""Demonstrate how to add metadata to a Photoshop document.
2+
3+
This example shows how to add and modify metadata properties of the active document,
4+
such as author, location, and title information.
5+
6+
Example:
7+
```python
8+
with Session(action="new_document") as ps:
9+
doc = ps.active_document
10+
doc.info.author = "John Doe"
11+
doc.info.title = "My Project"
12+
```
13+
14+
Note:
15+
- The script automatically creates a new document using the Session action parameter
16+
- The author is set to the current system username by default
17+
- You can view metadata in Photoshop via: File > File Info
18+
"""
219

320
# Import built-in modules
421
from __future__ import annotations
@@ -8,10 +25,16 @@
825
# Import local modules
926
from photoshop import Session
1027

28+
# Create a new Photoshop session and document
1129
with Session(action="new_document") as ps:
30+
# Get reference to active document
1231
doc = ps.active_document
13-
doc.info.author = os.getenv("USERNAME")
14-
doc.info.provinceState = "Beijing"
15-
doc.info.title = "My Demo"
32+
33+
# Set metadata properties
34+
doc.info.author = os.getenv("USERNAME") # Set author to system username
35+
doc.info.provinceState = "Beijing" # Set location information
36+
doc.info.title = "My Demo" # Set document title
37+
38+
# Print the metadata information
1639
ps.echo("Metadata of current active document:")
1740
ps.echo(doc.info)

examples/add_slate.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,69 @@
1-
"""Add slate information dynamically.
1+
"""Demonstrate how to add and update slate information in a Photoshop template.
22
3-
- Open template.
4-
- Update info.
5-
- Save as jpg.
6-
- Close current document.
3+
This example shows how to:
4+
1. Open a PSD template file
5+
2. Update text layers with dynamic information
6+
3. Save the result as a JPG file
7+
4. Display the saved file
78
9+
The script uses a template PSD file that contains text layers within a layer set
10+
named "template". It updates specific text layers with dynamic content like
11+
project name and current date.
12+
13+
Example:
14+
```python
15+
with Session(template_file, action="open") as ps:
16+
layer_set = ps.active_document.layerSets.getByName("template")
17+
for layer in layer_set.layers:
18+
if layer.kind == ps.LayerKind.TextLayer:
19+
layer.textItem.contents = dynamic_data[layer.textItem.contents]
20+
```
21+
22+
Note:
23+
- Requires a PSD template file with text layers in a layer set named "template"
24+
- Text layers should have placeholder text matching keys in the data dictionary
25+
- The script uses os.startfile which is Windows-specific
826
"""
927

1028
# Import built-in modules
1129
from __future__ import annotations
1230

1331
import os
14-
from datetime import datetime
15-
from tempfile import mkdtemp
32+
from datetime import datetime, timezone
1633
from pathlib import Path
17-
from datetime import timezone
34+
from tempfile import mkdtemp
1835

1936
# Import third-party modules
2037
import examples._psd_files as psd # Import from examples.
2138

2239
# Import local modules
2340
from photoshop import Session
2441

42+
# Get path to template PSD file
2543
PSD_FILE = psd.get_psd_files()
2644
slate_template = PSD_FILE["slate_template.psd"]
45+
46+
# Open template file in Photoshop
2747
with Session(slate_template, action="open", auto_close=True) as ps:
48+
# Get the layer set named "template"
2849
layer_set = ps.active_document.layerSets.getByName("template")
2950

51+
# Prepare dynamic data for text layers
3052
data = {
3153
"project name": "test_project",
3254
"datetime": datetime.now(tz=timezone.utc).strftime("%Y-%m-%d"),
3355
}
56+
57+
# Update text layers with dynamic content
3458
for layer in layer_set.layers:
3559
if layer.kind == ps.LayerKind.TextLayer:
3660
layer.textItem.contents = data[layer.textItem.contents.strip()]
3761

62+
# Save the document as JPG in a temporary directory
3863
jpg_file = Path(mkdtemp("photoshop-python-api")) / "slate.jpg"
3964
ps.active_document.saveAs(str(jpg_file), ps.JPEGSaveOptions())
4065
ps.echo(f"Save jpg to {jpg_file}")
66+
67+
# Open the saved JPG file (Windows-specific)
4168
# Note: os.startfile is Windows-specific, consider using a cross-platform solution
4269
os.startfile(str(jpg_file))
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
11
"""Add event for Photoshop start application.
22
3-
In the current example, every time we start photoshop it will
4-
alert "Start Application Event".
3+
This example demonstrates how to add an event listener that triggers when Photoshop starts.
4+
Every time Photoshop is launched, it will display an alert message saying "Start Application Event".
55
6-
Just like you manually in Script> Script Events Manager to enable the event.
6+
This is equivalent to manually setting up an event in Photoshop through:
7+
File > Scripts > Script Events Manager
78
9+
Example:
10+
```python
11+
with Session() as ps:
12+
root = Path(mkdtemp())
13+
jsx_file = root / "event.jsx"
14+
jsx_file.write_text('alert("Start Application event.")')
15+
ps.app.notifiers.add(ps.EventID.Notify, str(jsx_file))
16+
```
17+
18+
Note:
19+
The event will persist even after the script finishes running.
20+
To remove the event, use the Script Events Manager in Photoshop.
821
"""
922

1023
# Import built-in modules
1124
from __future__ import annotations
1225

13-
import os
14-
from tempfile import mkdtemp
1526
from pathlib import Path
27+
from tempfile import mkdtemp
1628

1729
# Import local modules
1830
from photoshop import Session
1931

32+
# Create a new Photoshop session
2033
with Session() as ps:
34+
# Create a temporary directory to store the JSX script
2135
root = Path(mkdtemp())
2236
jsx_file = root / "event.jsx"
37+
38+
# Write the JavaScript code that will be executed when Photoshop starts
2339
jsx_file.write_text('alert("Start Application event.")')
40+
41+
# Add the event notifier to Photoshop
42+
# EventID.Notify is triggered when Photoshop starts
2443
ps.app.notifiers.add(ps.EventID.Notify, str(jsx_file))
44+
45+
# Confirm the event was added successfully
2546
ps.echo("Add event done.")

0 commit comments

Comments
 (0)