From 141c0244e49c22289ef7af32bb8d7c58c2fc467d Mon Sep 17 00:00:00 2001 From: Rhet Turnbull Date: Wed, 7 Jul 2021 06:59:40 -0700 Subject: [PATCH] Added --selected, closes #489 --- README.md | 7 +++++-- osxphotos/_version.py | 2 +- osxphotos/cli.py | 11 +++++++++++ osxphotos/photosdb/photosdb.py | 13 +++++++++++++ osxphotos/queryoptions.py | 1 + 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f064f86d..4f1c9e05 100644 --- a/README.md +++ b/README.md @@ -753,6 +753,9 @@ Options: more than one regular expression match by repeating '--regex' with different arguments. + --selected Filter for photos that are currently selected + in Photos. + --query-eval CRITERIA Evaluate CRITERIA to filter photos. CRITERIA will be evaluated in context of the following python list comprehension: `photos = [photo @@ -1776,7 +1779,7 @@ Substitution Description {lf} A line feed: '\n', alias for {newline} {cr} A carriage return: '\r' {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 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}| |{cr}|A carriage return: '\r'| |{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| |{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| diff --git a/osxphotos/_version.py b/osxphotos/_version.py index dd89debb..edccfece 100644 --- a/osxphotos/_version.py +++ b/osxphotos/_version.py @@ -1,3 +1,3 @@ """ version info """ -__version__ = "0.42.60" +__version__ = "0.42.61" diff --git a/osxphotos/cli.py b/osxphotos/cli.py index 3d688000..4a069445 100644 --- a/osxphotos/cli.py +++ b/osxphotos/cli.py @@ -536,6 +536,11 @@ def QUERY_OPTIONS(f): "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.", ), + o( + "--selected", + is_flag=True, + help="Filter for photos that are currently selected in Photos.", + ), o( "--query-eval", metavar="CRITERIA", @@ -1182,6 +1187,7 @@ def export( min_size, max_size, regex, + selected, query_eval, query_function, duplicate, @@ -1345,6 +1351,7 @@ def export( min_size = cfg.min_size max_size = cfg.max_size regex = cfg.regex + selected = cfg.selected query_eval = cfg.query_eval query_function = cfg.query_function duplicate = cfg.duplicate @@ -1663,6 +1670,7 @@ def export( min_size=min_size, max_size=max_size, regex=regex, + selected=selected, query_eval=query_eval, function=query_function, duplicate=duplicate, @@ -2071,6 +2079,7 @@ def query( min_size, max_size, regex, + selected, query_eval, query_function, add_to_album, @@ -2105,6 +2114,7 @@ def query( min_size, max_size, regex, + selected, duplicate, ] exclusive = [ @@ -2235,6 +2245,7 @@ def query( query_eval=query_eval, function=query_function, regex=regex, + selected=selected, duplicate=duplicate, ) diff --git a/osxphotos/photosdb/photosdb.py b/osxphotos/photosdb/photosdb.py index bd788689..3d8e60f3 100644 --- a/osxphotos/photosdb/photosdb.py +++ b/osxphotos/photosdb/photosdb.py @@ -17,6 +17,7 @@ from pprint import pformat from typing import List import bitmath +import photoscript from .._constants import ( _DB_TABLE_NAMES, @@ -3324,6 +3325,18 @@ class PhotosDB: elif options.no_location: 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: for function in options.function: photos = function[0](photos) diff --git a/osxphotos/queryoptions.py b/osxphotos/queryoptions.py index c5e85641..23c27505 100644 --- a/osxphotos/queryoptions.py +++ b/osxphotos/queryoptions.py @@ -83,6 +83,7 @@ class QueryOptions: location: Optional[bool] = None no_location: Optional[bool] = None function: Optional[List[Tuple[callable, str]]] = None + selected: Optional[bool] = None def asdict(self): return asdict(self)