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

Rewrite osxmetadata to use native Objective-C methods where possible #62

Closed
RhetTbull opened this issue Sep 24, 2022 · 2 comments
Closed
Labels
enhancement New feature or request

Comments

@RhetTbull
Copy link
Owner

Reference this. Also, see #10 and #61.

@RhetTbull RhetTbull added the enhancement New feature or request label Sep 24, 2022
@RhetTbull
Copy link
Owner Author

Tags can be set like this:

""""Set Finder tags on files and folders."""

import sys

from Foundation import NSURL, NSURLTagNamesKey, NSURLLocalizedLabelKey

if __name__ == "__main__":
    file = sys.argv[1]
    tags = sys.argv[2:]
    tags = ["Tag5\n5"]
    url = NSURL.fileURLWithPath_(file)
    if not url.setResourceValues_error_(dict(NSURLTagNamesKey=tags), None):
        print(f"Error setting tags {tags} on {file}")
        sys.exit(1)
    print(f"Tags {tags} set on {file}")
    label_name = url.getResourceValue_forKey_error_(None, NSURLLocalizedLabelKey, None)
    print(f"Label name: {label_name}")

@RhetTbull
Copy link
Owner Author

RhetTbull commented Sep 26, 2022

Reference: https://github.com/hugocornejo/Locker-Room/blob/cbe8788ef1216be22b1cf7d2efc42b49f61ac7ed/DribbbleLikeDownloader.m

-(void)setFinderComment:(NSString*)comment forFile:(NSString*)path
{
	@try {
		static FinderApplication *finderApp = nil;
		if (finderApp == nil) {
			finderApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
		}
		NSURL *location = [NSURL fileURLWithPath:path];
		FinderItem *item = [[finderApp items] objectAtLocation:location];
		item.comment = comment;
	}
	@catch (NSException *ex) {
		NSLog(@"Unable to set finder comment for %@: %@", path, ex);
	}
}

This still involves scripting but uses direct communication with Finder instead of calling AppleScript.

@RhetTbull RhetTbull mentioned this issue Oct 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant