Applescript, show all files with tag

蓝咒 提交于 2020-01-07 04:56:05

问题


I am trying to build an applescript which will open show all files tagged with the school class I am currently in, based on current time. For instance, if the time is 10 AM on a Tuesday, it will show all my files tagged with Chemistry. Now, I have made the AppleScript to get the correct name of the class.

Now I need to tell Finder to open the files with the correct tags. How would I do this?

Basicaly, something like this:

set tagged to "Tag:Chemistry"
tell application "Finder"
    reveal tagged
    activate
end tell

Ofcourse, tagged here would be dynamically assigned.


回答1:


You can use the GUI Scripting to click on left-pane in the finder window.

Because this script use GUI Scripting to control the user-interface, you must add the applet to an approval list, displayed in the Security & Privacy system preference pane.

set tagName to "Chemistry" -- the full tag's name
my showFinderWindow(tagName)

on showFinderWindow(thisTag)
    tell application "Finder" to if not (exists Finder window 1) then make new Finder window
    tell application "System Events"
        tell process "Finder"
            repeat with i in windows
                tell outline 1 of scroll area 1 of splitter group 1 of i to if exists then
                    tell (first row whose value of its static text 1 is thisTag) to if exists then perform action "AXOpen" of static text 1
                    exit repeat
                end if
            end repeat
        end tell
    end tell
end showFinderWindow



回答2:


The way way I would do this in the GUI is with Smart Folders. Go in the Finder, click on File->New Smart Folder and a new Finder window appears. Click on + at the top right beside Save and change Kind at top left of window to Tags. Beside Tags choose contains and then type Chemistry. Click Save at top right and call it Chemistry Stuff and allow it to be added to the side-bar. Then it will appear on the left of all Finder windows.

In the shell/Terminal, where I usually reside, I use tag for that purpose. I installed it with homebrew using brew install tag. Then I can do tag -f sometag and it lists all the files that are tagged sometag.

tag - A tool for manipulating and querying file tags.
  usage:
    tag -a | --add <tags> <file>...     Add tags to file
    tag -r | --remove <tags> <file>...  Remove tags from file
    tag -s | --set <tags> <file>...     Set tags on file
    tag -m | --match <tags> <file>...   Display files with matching tags
    tag -l | --list <file>...           List the tags on file
    tag -f | --find <tags>              Find all files with tags
  <tags> is a comma-separated list of tag names; use * to match/find any tag.
  additional options:
        -v | --version      Display version
        -h | --help         Display this help
        -n | --name         Turn on filename display in output (default)
        -N | --no-name      Turn off filename display in output (list, find, match)
        -t | --tags         Turn on tags display in output (find, match)
        -T | --no-tags      Turn off tags display in output (list)
        -g | --garrulous    Display tags each on own line (list, find, match)
        -G | --no-garrulous Display tags comma-separated after filename (default)
        -H | --home         Find tagged files only in user home directory
        -L | --local        Find tagged files only in home + local filesystems (default)
        -R | --network      Find tagged files in home + local + network filesystems
        -0 | --nul          Terminate lines with NUL (\0) for use with xargs -0



回答3:


You can use the command line tool "mdfind" to search for tagged files using "kMDItemUserTags". So that's how I would find the tagged files.

The other part of your problem is finding the proper tag for the current time-of-day. I would use applescript records for this. For example you can make a record like this:

{theTag:"Chemistry", startTime:date ("10:00 AM"), endTime:date ("11:00 AM")}

So if you make a list of records you can loop through them and compare the start and end times of each record in the list to the current date.

Finally, once you have that and assuming you found some files with mdfind then you can just loop through the found files and ask the Finder to open them.

Try this. Note you have to supply your values for "requirementsRecords" and "searchFolder" in the user variables section.

-- user variables
set requirementsRecords to {{theTag:"Chemistry", startTime:date ("10:00 AM"), endTime:date ("11:00 AM")}, {theTag:"Math", startTime:date ("11:00 AM"), endTime:date ("12:00 PM")}}
set searchFolder to path to desktop


-- find the tag information given the current time
set now to current date
set thisTag to missing value
repeat with i from 1 to count of requirementsRecords
    set thisRecord to item i of requirementsRecords
    set thisStartTime to startTime of thisRecord
    set thisEndTime to endTime of thisRecord

    if now is greater than or equal to thisStartTime and now is less than or equal to thisEndTime then
        set thisTag to theTag of thisRecord
        exit repeat
    end if
end repeat
if thisTag is missing value then error "Could not find a tag for the current time!"

-- search the search folder for files with the tag
set cmd to "mdfind -onlyin " & quoted form of POSIX path of searchFolder & " \"kMDItemUserTags == " & quoted form of thisTag & "\""
set resultsList to paragraphs of (do shell script cmd)
if resultsList is {} then error "Could not find any files tagged \"" & thisTag & "\" in the search folder"

-- open the found files with the Finder
repeat with anItem in resultsList
    set thisFile to POSIX file anItem
    tell application "Finder" to open thisFile
end repeat


来源:https://stackoverflow.com/questions/27932459/applescript-show-all-files-with-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!