Added --selected, closes #489

This commit is contained in:
Rhet Turnbull
2021-07-07 06:59:40 -07:00
parent 7e0276beb7
commit 141c0244e4
5 changed files with 31 additions and 3 deletions

View File

@@ -753,6 +753,9 @@ Options:
more than one regular expression match by more than one regular expression match by
repeating '--regex' with different arguments. repeating '--regex' with different arguments.
--selected Filter for photos that are currently selected
in Photos.
--query-eval CRITERIA Evaluate CRITERIA to filter photos. CRITERIA --query-eval CRITERIA Evaluate CRITERIA to filter photos. CRITERIA
will be evaluated in context of the following will be evaluated in context of the following
python list comprehension: `photos = [photo python list comprehension: `photos = [photo
@@ -1776,7 +1779,7 @@ Substitution Description
{lf} A line feed: '\n', alias for {newline} {lf} A line feed: '\n', alias for {newline}
{cr} A carriage return: '\r' {cr} A carriage return: '\r'
{crlf} a carriage return + line feed: '\r\n' {crlf} a carriage return + line feed: '\r\n'
{osxphotos_version} The osxphotos version, e.g. '0.42.59' {osxphotos_version} The osxphotos version, e.g. '0.42.61'
{osxphotos_cmd_line} The full command line used to run osxphotos {osxphotos_cmd_line} The full command line used to run osxphotos
The following substitutions may result in multiple values. Thus if specified for The following substitutions may result in multiple values. Thus if specified for
@@ -3608,7 +3611,7 @@ The following template field substitutions are availabe for use the templating s
|{lf}|A line feed: '\n', alias for {newline}| |{lf}|A line feed: '\n', alias for {newline}|
|{cr}|A carriage return: '\r'| |{cr}|A carriage return: '\r'|
|{crlf}|a carriage return + line feed: '\r\n'| |{crlf}|a carriage return + line feed: '\r\n'|
|{osxphotos_version}|The osxphotos version, e.g. '0.42.59'| |{osxphotos_version}|The osxphotos version, e.g. '0.42.61'|
|{osxphotos_cmd_line}|The full command line used to run osxphotos| |{osxphotos_cmd_line}|The full command line used to run osxphotos|
|{album}|Album(s) photo is contained in| |{album}|Album(s) photo is contained in|
|{folder_album}|Folder path + album photo is contained in. e.g. 'Folder/Subfolder/Album' or just 'Album' if no enclosing folder| |{folder_album}|Folder path + album photo is contained in. e.g. 'Folder/Subfolder/Album' or just 'Album' if no enclosing folder|

View File

@@ -1,3 +1,3 @@
""" version info """ """ version info """
__version__ = "0.42.60" __version__ = "0.42.61"

View File

@@ -536,6 +536,11 @@ def QUERY_OPTIONS(f):
"For example, to find photos in an album that begins with 'Beach': '--regex \"^Beach\" \"{album}\"'. " "For example, to find photos in an album that begins with 'Beach': '--regex \"^Beach\" \"{album}\"'. "
"You may specify more than one regular expression match by repeating '--regex' with different arguments.", "You may specify more than one regular expression match by repeating '--regex' with different arguments.",
), ),
o(
"--selected",
is_flag=True,
help="Filter for photos that are currently selected in Photos.",
),
o( o(
"--query-eval", "--query-eval",
metavar="CRITERIA", metavar="CRITERIA",
@@ -1182,6 +1187,7 @@ def export(
min_size, min_size,
max_size, max_size,
regex, regex,
selected,
query_eval, query_eval,
query_function, query_function,
duplicate, duplicate,
@@ -1345,6 +1351,7 @@ def export(
min_size = cfg.min_size min_size = cfg.min_size
max_size = cfg.max_size max_size = cfg.max_size
regex = cfg.regex regex = cfg.regex
selected = cfg.selected
query_eval = cfg.query_eval query_eval = cfg.query_eval
query_function = cfg.query_function query_function = cfg.query_function
duplicate = cfg.duplicate duplicate = cfg.duplicate
@@ -1663,6 +1670,7 @@ def export(
min_size=min_size, min_size=min_size,
max_size=max_size, max_size=max_size,
regex=regex, regex=regex,
selected=selected,
query_eval=query_eval, query_eval=query_eval,
function=query_function, function=query_function,
duplicate=duplicate, duplicate=duplicate,
@@ -2071,6 +2079,7 @@ def query(
min_size, min_size,
max_size, max_size,
regex, regex,
selected,
query_eval, query_eval,
query_function, query_function,
add_to_album, add_to_album,
@@ -2105,6 +2114,7 @@ def query(
min_size, min_size,
max_size, max_size,
regex, regex,
selected,
duplicate, duplicate,
] ]
exclusive = [ exclusive = [
@@ -2235,6 +2245,7 @@ def query(
query_eval=query_eval, query_eval=query_eval,
function=query_function, function=query_function,
regex=regex, regex=regex,
selected=selected,
duplicate=duplicate, duplicate=duplicate,
) )

View File

@@ -17,6 +17,7 @@ from pprint import pformat
from typing import List from typing import List
import bitmath import bitmath
import photoscript
from .._constants import ( from .._constants import (
_DB_TABLE_NAMES, _DB_TABLE_NAMES,
@@ -3324,6 +3325,18 @@ class PhotosDB:
elif options.no_location: elif options.no_location:
photos = [p for p in photos if p.location == (None, None)] photos = [p for p in photos if p.location == (None, None)]
if options.selected:
# photos selected in Photos app
try:
# catch AppleScript errors as the scripting interfce to Photos is flaky
selected = photoscript.PhotosLibrary().selection
selected_uuid = [p.uuid for p in selected]
photos = [p for p in photos if p.uuid in selected_uuid]
except Exception:
# no photos selected or a selected photo was "open"
# selection only works if photos selected in main media browser
photos = []
if options.function: if options.function:
for function in options.function: for function in options.function:
photos = function[0](photos) photos = function[0](photos)

View File

@@ -83,6 +83,7 @@ class QueryOptions:
location: Optional[bool] = None location: Optional[bool] = None
no_location: Optional[bool] = None no_location: Optional[bool] = None
function: Optional[List[Tuple[callable, str]]] = None function: Optional[List[Tuple[callable, str]]] = None
selected: Optional[bool] = None
def asdict(self): def asdict(self):
return asdict(self) return asdict(self)