1

What I would love to be able to do, is the opposite of what this person asked about; I want to ADD the "where from" text (in my case, a URL I could copy-paste from a browser tab) for screenshots I take of genealogical documents online. I realize what I'm asking isn't the intent of this metadata field, but for my own use, it would be extremely useful. If Info Sheet via "get info" (command+I) could accommodate a text-box for this field, that would be surreal, but any alternative method is worth knowing about. The goal is that this could be done very quickly and without too much effort after I copy the URL.

This particular question relates to this question I posted a short while ago, which is far more important; a way to auto-launch Info Sheet after a screenshot is captured, as a reminder, to force me to paste the URL into the Comments box (which isn't the perfect spot for my needs but nevertheless far better than not having the URL at all). Thanks all! I'm a bit of an idiot with code but learn a great deal from all of you.

0

2 Answers 2

1

Along with the information window, a dialog can be put up to get the whereFrom text, with a little AppleScriptObjC (since a shell script has been posted in another answer) to add it. Extending the Automator folder action in my answer to your previous topic, the replacement Run AppleScript action would look something like:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property useBinary : missing value

on run {input, parameters}
    repeat with anItem in input
        tell application "Finder"
            activate
            open information window of anItem
        end tell

        set response to (display dialog "Enter text to add to whereFroms:" default answer "" buttons {"Enter", "Skip"} default button 2)
        if button returned of response is "Enter" then
            addWhereFrom(text returned of response, anItem)
        end if
    end repeat

    return input
end run

to addWhereFrom(newItem, filePath) -- add to existing whereFroms, trimming duplicates
    set whereFroms to readWhereFroms(filePath)
    set end of whereFroms to (newItem as text)
    set whereFroms to (current application's NSOrderedSet's orderedSetWithArray:whereFroms)'s allObjects()
    writeWhereFroms(whereFroms, filePath)
end addWhereFrom

to readWhereFroms(filePath) -- get a list of whereFroms from the extended attribute
    set filePath to quoted form of POSIX path of filePath
    set attribute to missing value
    set useBinary to missing value -- keep track of which it is
    try -- get existing attribute as property list
        set attribute to (do shell script "xattr -p com.apple.metadata:kMDItemWhereFroms " & filePath & "  | xxd -r -p | plutil -convert xml1 -o - -") -- convert from binary
        set useBinary to true
    on error -- oops, not a binary plist, so try XML
        try -- skip error if no attribute
            set attribute to (do shell script "xattr -p com.apple.metadata:kMDItemWhereFroms " & filePath)
            set useBinary to false
        end try
    end try
    if attribute is in {missing value, ""} then return {}
    # deserialize the list from the property list string
    set theData to (current application's NSString's stringWithString:attribute)'s dataUsingEncoding:(current application's NSUTF8StringEncoding)
    return (current application's NSPropertyListSerialization's propertyListWithData:theData options:(current application's NSPropertyListMutableContainersAndLeaves) format:(missing value) |error|:(missing value)) as list
end readWhereFroms

to writeWhereFroms(theList, filePath) -- set the extended attribute to a list of whereFroms
    set filePath to quoted form of POSIX path of filePath
    # serialize the list into a property list string
    set theData to (current application's NSPropertyListSerialization's dataWithPropertyList:theList format:(current application's NSPropertyListXMLFormat_v1_0) options:0 |error|:(missing value))
    set plist to (current application's NSString's alloc's initWithData:theData encoding:(current application's NSUTF8StringEncoding)) as text
    if useBinary is false then -- set XML plist
        do shell script "xattr -w com.apple.metadata:kMDItemWhereFroms " & quoted form of plist & space & filePath
    else -- convert and set binary plist
        set bplist to do shell script "echo " & quoted form of plist & " | plutil -convert binary1 -o - - | xxd -p"
        do shell script "xattr -w -x com.apple.metadata:kMDItemWhereFroms " & bplist & space & filePath
    end if
end writeWhereFroms

To use the above script by itself, just remove {input, parameters} from the run handler declaration, and replace the input variable or set it to your own list of file items, for example from a choose file dialog.

1
  • You've outdone yourself! Reviewing this now. Can't wait to try it. Just lost power here (high wind) so have to take care of some things before reporting back, which I will do asap. Thanks so much for your time on this. I really appreciate it!
    – Ryan
    Commented Mar 29, 2020 at 20:46
0

If you want to add Where from: info under More info: on a file's Get Info window, i.e., as if pressing ⌘I on a selected file in Finder and seeing it there, then the following example in Terminal can do that:

xattr -w com.apple.metadata:kMDItemWhereFroms 'https://apple.stackexchange.com/questions/386599/manually-enter-where-from-text-for-screenshot-metadata' "/path/to/file"

The example above would write the URL of this web page to the file specified by: "/path/to/file"

Note that there are a number of ways that automation could be employed to do this so it wouldn't need to be done manually.

enter image description here


Note: This is a simple example of how to write the target meta-data to the file and as written would overwrite existing meta-data of that type. Additional coding required to append new meta-data to existing meta-data of the same type, however, this is deemed outside the scope of the expressed use case.

2
  • WOW!!! Goal would be to simplify or automate this further. NP about lost metadata as these are only screenshots and all I need is date stamp. I also don't care if the written URL into Where From is bad due to java-enabled sites etc. This is just a helper tool, and maybe fore DEVONthink integration. If I could automate that long line you authored somehow, that would be really ideal: i.e. by writing the active Chrome browser URL into Where From, regardless of what picture the screenshot takes (and leave it blank if Chrome isn't open). Thanks again. This is exciting!
    – Ryan
    Commented Mar 29, 2020 at 20:41
  • @Ryan, The caveat about overwriting existed meta_data is just for the com.apple.metadata:kMDItemWhereFroms but it shouldn't exist in a screen shot anyway. I added it because it was a point to distinguish for thoroughness of the example code. :) Here are some lines of example AppleScript code for you: tell application "Google Chrome" to set thisURL to URL of active tab of front window and do shell script "xattr -w com.apple.metadata:kMDItemWhereFroms" & space & thisURL's quoted form & space & "/path/to/file" Commented Mar 29, 2020 at 21:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .