Setting Finder label via AppleScript on High Sierra - macos

I'm trying to use AppleScript to set the label of a file to a specific colour; or rather, I already have a script that used to work, but ever since upgrading to High Sierra it no longer does.
I've stripped it down to the absolute basics (always sets label to green):
on run theArguments
set theFile to POSIX file (item 1 of theArguments)
tell application "Finder" to set (theFile's label index) to 6
end run
If you save this to a file (green.scpt) then you can run it from Terminal with the following command:
osascript ~/Downloads/green.scpt ~/Downloads/green.scpt
(substitute the paths for wherever you store the script)
This should turn the script's label in the Finder to green, but doesn't (at least on High Sierra), instead giving the following error message:
/Users/haravikk/Downloads/green.scpt: execution error: Finder got an error: Can’t set label index of file "Users:haravikk:Downloads:green.scpt" to 6. (-10006)
Am I doing something wrong here? If not, and this is a bug, then is there some other way to change a file's label via script?

You can try to use tell application before change label index
on run theArguments
tell application "Finder"
set thisItem to POSIX file theArguments as alias
if label index of thisItem is not 6 then
set the label index of thisItem to 6
end if
end tell
end run

This works for me (added "as alias"):
on run theArguments
set theFile to POSIX file (item 1 of theArguments) as alias
tell application "Finder" to set (theFile's label index) to 6
end run
Note though that labels are long gone, and have been replaced with tags.

Related

Script to find active wallpaper file in Finder under macOS Catalina

I used to use two AppleScript scripts to find out the file name of the actual wallpaper image from desktop 1 and desktop 2 (dual monitor mode) under macOS Mojave. One script for the main monitor and another one for the second monitor. Under macOS Catalina the scripts are not working anymore.
Here is the script:
tell application "System Events"
set posix_path to (pictures folder of desktop 2)
set picPath to (POSIX file posix_path) as string
end tell
set thePictures to (do shell script "sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.picture_id=7 and preferences.data_id=data.ROWID\"")
set fullPath to picPath as string
set rotationImage to fullPath & thePictures
tell application "Finder"
try
set aliasItem to item rotationImage
if class of aliasItem is alias file then
reveal original item of aliasItem
end if
end try
end tell
Here is the error message:
tell application "System Events"
get pictures folder of desktop 1
--> "/Users/peter/Library/Caches/com.apple.preference.desktopscreeneffect.desktop/69948584/DSKPhotosRootSource"
get POSIX file "/Users/peter/Library/Caches/com.apple.preference.desktopscreeneffect.desktop/69948584/DSKPhotosRootSource"
--> error number -1728 from POSIX file "/Users/peter/Library/Caches/com.apple.preference.desktopscreeneffect.desktop/69948584/DSKPhotosRootSource"
end tell
tell current application
do shell script "sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.picture_id=1 and preferences.data_id=data.ROWID\""
--> "13725B"
end tell
tell application "Finder"
get item "Macintosh HD:Users:peter:Library:Caches:com.apple.preference.desktopscreeneffect.desktop:69948584:DSKPhotosRootSource13725B"
--> error number -1728 from item "Macintosh HD:Users:peter:Library:Caches:com.apple.preference.desktopscreeneffect.desktop:69948584:DSKPhotosRootSource13725B"
end tell
Tried to find the problem but couldn't find a solution. I am not an experienced AppleScript writer. Hope somebody can help.
On Catalina and Mojave, I'm able to get the current wallpaper by using a sqlite command similar to yours:
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "select * from data" | tail -2
Or in Applescript:
do shell script "sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db 'select * from data' | tail -2"
On my Mac, the last 2 items in the data table appear to be some combination of the most recently set wallpaper and last 2 displays that were most recently set, so I tail the list. Like you, I'm also using a large folder of wallpapers and I have it set to change every 30 minutes. As long as I don't manually change a wallpaper, the last 2 items are consistently the names of the 2 active wallpapers because both monitors change at the same time every 30 minutes.
Something to note: When you are using a Folder with the "Change picture:" checkbox checked, the items returned from select * data is just the file name (i.e., wallpaper.jpg). If you've set the wallpaper to a single image, the item returned from the select command is the full path of the image (i.e., /path/to/folder/wallpaper.jpg). Since I'm using a folder, so I get just the image names in my select results. Then I can split the 2 names by newline to get each wallpaper name in an array and then open them. Here's my whole script:
#!/bin/bash
#reads and opens the last 2 items from the 'data' table in the desktoppicture.db sqlite db
last_two=`sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "select * from data" | tail -2`
IFS=$'\n' read -rd '' -a y <<<"$last_two"
#echo "first is ${y[0]}"
#echo "second is ${y[1]}"
open /Users/myusername/Pictures/Desktop\ Pictures/${y[0]}
open /Users/myusername/Pictures/Desktop\ Pictures/${y[1]}
I realize you asked an AppleScript question and this is largely a bash script answer, but the do shell script item at the top of my answer should get you going enough to be able to capture and manipulate the image names inside of AppleScript.
Just to close this out, I use this bash script in Alfred using a keyword "retire" to retire a wallpaper I'm tired of. I type the keyword, this script runs to open the images in Preview, another script runs to open my Desktop Pictures folder and a Desktop Pictures Retired folder and then I manually move the photo into the Retired folder.
If you currently have two monitors connected and you're simply looking to retrieve the names of the Desktop Wall Papers for each, this following Apple script code should be what you're looking for.
tell application "System Events"
set everyDesktop to desktops
set desktopOnePicture to picture of item 1 of everyDesktop
set desktopTwoPicture to picture of item 2 of everyDesktop
end tell
I can't guarantee the code below will fair any better than the solution already posted, but it should, in theory, target desktop images per screen (monitor) rather than by desktop (space). However, I don't have Mojave or Catalina, or a computer to test this out:
use framework "AppKit"
property NSScreen : a reference to NSScreen in the current application
property NSWorkspace : a reference to NSWorkspace in the current application
property currentScreen : a reference to the mainScreen of NSScreen
on screen(i as integer)
local i
if i = 0 then return the currentScreen()
return NSScreen's screens()'s item i
end screen
on desktopImageURLForScreen:(i as integer)
local i
set S to screen(i)
tell NSWorkspace's sharedWorkspace() to return the ¬
desktopImageURLForScreen_(S) as «class furl»
end desktopImageURLForScreen:
return the POSIX path of my desktopImageURLForScreen:0
The bottom line is the one you will most likely want to experiment with, by changing the index number passed to the handler. If you have three monitors, then they would each be identified by one of the indices 1, 2, or 3 (I can't predict how index corresponds to arrangement of monitors). Index 0 will always refer to the screen that currently has keyboard focus.

Applescript - save argument to text file

I need to write a simple applescript that will be opened with an argument. That argument will be a file path. I need it to save this file path to a text file. I want to avoid using shell script.
I'm finding this surprisingly difficult, despite having done it in .sh and .bat already
Thanks very much!
Adam
//// the current code which is not working is below. the code begins with "on run" and ends with "end run" but for some reason this isn't being highlighted as code
on run argv
set this_PATH to (POSIX file argv)
tell application "TextEdit"
activate
make new document
set text of document 1 to this_PATH as text
save document 1 in "/Users/adamparkinson/Desktop/date.txt"
end tell
end run
I made this work:
#!/usr/bin/osascript
on run argv
tell application "TextEdit"
activate
make new document
tell document 1
set its text to (item 1 of argv as text)
save in posix file (item 1 of argv as text )
end tell
end tell
end run
I called it like this:
./SavePath.sh /Users/Me/Desktop/junk/Newstuff.txt (Of course I used chmod u+x SavePath.sh to make it executable.)
If this works for you, then please check of my answer as answered. :)
with applescript it is very easy to create application Drag and Drop without any script shell below a version Drag and Drop of your script
on open draggedItems
set this_PATH to quoted form of POSIX path of draggedItems
repeat with currentItem in draggedItems
tell application "TextEdit"
activate
set doc to open POSIX file "/Users/adamparkinson/Desktop/date.txt"
make new word at end of text of doc with data (this_PATH as text) & return
end tell
end repeat
end open

Applescript setting desktop picture to file in current directory

I'm attempting to set the current desktop picture to an image in the current directory of the script. For example, if my folder structure is:
~/Documents/Scripts/DesktopImageScript/image.jpg
With the script in the same directory, I want to be able to set the desktop image to the image.jpg without directly referring to the folder structure. The code I am currently using to fetch the current directory is:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
The issue isn't in that code as I can run the following command with the expected and correct results:
do shell script "echo " & app_directory
I believe the issue is in the code I'm using to set the desktop image:
tell application "Finder"
set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg"))
end tell
The error I receive when I try to run the script is:
error "Finder got an error: AppleEvent handler failed." number -10000
Not really sure what could be causing the error or how to fix it. Any help is appreciated. The full script is below:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
tell application "Finder"
set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg"))
end tell
Solved by using code from another answer on StackOverflow.
tell application "System Events"
set theDesktops to a reference to every desktop
repeat with x from 1 to (count theDesktops)
set picture of item x of the theDesktops to app_directory & "/image.jpg"
end repeat
end tell

How to change Current Working directory using AppleScript

What would be a simplest way to change a current working directory (cwd) using AppleScript. It would be equivalent to OS's cd or Python's os.chdir. Would be great if a destination directory if doesn't exist would be created on a fly (but that would be highly optional).
If you are looking to use with an applications save dialog...
set filePath to "path/to/my/file"
tell application "System Events"
-- Once save dialog is open
keystroke "g" using {shift down, command down}
delay 1
set value of text field 1 of sheet 1 of sheet 1 of window 1 to filePath
end tell
You might be thinking of doing something like this:
tell application "Finder"
# ^^^ or whatever application you want to control up there
# to get to a file...
set filePath to POSIX file "/Users/username/Documents/new.mp3"
# to get to a folder...
set testFolder to POSIX path "/Users/username/test"
if (exists (folder testFolder)) then
say "folder exists"
else
make new folder at testFolder
endif
end tell
I'm basing my answer off the answers on this page and this related question (or this one).

OS X 10.8.4: Applescript giving syntax error for 'property list file'

I want to find whether or not a specific version of Firefox was installed on a Mac that may have one or more different versions of Firefox installed. I can't simply use version of application "Firefox" because I may miss alternate installations of Firefox that way. My Applescript code so far is:
tell application "Finder"
set firefox_list to paragraphs of (do shell script "find /Applications -name Firefox.app")
repeat with ff_installed in firefox_list
-- get version
set ff_ins_info to property list file (POSIX path of ff_installed & "/Contents/Info.plist")
set ff_ins_version to (value of property list item "Bundle version" of ff_ins_info)
display dialog ff_ins_version
end repeat
end tell
Unfortunately I get an error message when I try to compile: Syntax Error: Expected expression but found “property”. The best I can make out is that Applescript doesn't seem to recognize property list file as an object. As I understand it, this should work after 10.5.
I've considered writing a shell script to look up the version number using Apple's PlistBuddy utility, but that seems even more complicated than this approach. Am I left with greping through Plist XML directly at this point?
Thanks in advance for any suggestions!
I made 3 changes to get it working:
replaced tell application "Finder" with tell application "System Events"
left out POSIX path of
replaced Bundle version with CFBundleVersion
This is the final version
tell application "System Events"
set firefox_list to paragraphs of (do shell script "find /Applications -name Firefox.app")
repeat with ff_installed in firefox_list
-- get version
set ff_ins_info to property list file (ff_installed & "/Contents/Info.plist")
set ff_ins_version to (value of property list item "CFBundleVersion" of ff_ins_info)
display dialog ff_ins_version
end repeat
end tell

Resources