geoffgarside (owner)

Revisions

gist: 48202 Download_button fork
public
Public Clone URL: git://gist.github.com/48202.git
Nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env nush
; Useful script for when you have a rotating background on Mac OS X
; this script will tell you which image is currently being displayed.
; If you add -rm when calling the command then it will provide you with
; prompt to let you delete the background.
;
; Installation:
; Copy this into something like ~/bin/current_background and then
; $ chmod 750 ~/bin/current_background
; then use :D
;
; Bugs:
; 1. When prompting for user input I can't seem to flush stdout manually
; so I have to print a new line character in order to show the prompt.
;
 
(set processInfo (NSProcessInfo processInfo))
(set args (processInfo arguments))
 
(if (and (eq (args count) 3) (eq (args objectAtIndex:2) "-rm"))
    (set shouldOfferToRemove t)
    (else (set shouldOfferToRemove nil)))
 
;; Get the dictionary
(set plist ("~/Library/Preferences/com.apple.desktop.plist" stringByExpandingTildeInPath))
(set prefs (NSDictionary dictionaryWithContentsOfFile:plist))
 
;; Drill down to the dictionary key we care about
(set default ((prefs valueForKey:"Background") valueForKey:"default"))
 
;; Extract the fields we need
(set changePath (default valueForKey:"ChangePath"))
(set currentImage (default valueForKey:"LastName"))
 
;; Print the information
(puts "Current desktop image is #{currentImage}")
 
;; Offer to delete
(if (eq t shouldOfferToRemove)
    (print "Are you sure you want to delete #{currentImage} [y/N]: \n")
    (set input (((NSString alloc)
        initWithData:((NSFileHandle fileHandleWithStandardInput) availableData)
        encoding:NSUTF8StringEncoding)
            stringByTrimmingCharactersInSet:
                (NSCharacterSet whitespaceAndNewlineCharacterSet)))
    (if (or (input isEqualToString:"y") (input isEqualToString:"Y"))
        (set fm (NSFileManager defaultManager))
        (set path (changePath stringByAppendingPathComponent:currentImage))
        (if (fm removeFileAtPath:path handler:nil)
            (puts "Background #{currentImage} has been deleted")
            (else (puts "Background #{currentImage} could not be deleted")))
        (else
            (puts "Background #{currentImage} has not been deleted"))))
 
 
Bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/sh
# Useful if you have a directory of images which you use
# for a rotating desktop picture on Mac OS X. When run
# this script will print out the name of the image
#
# The DIR is set twice as it has a space in it and the
# first command won't really find it properly.
DIR=`defaults read com.apple.desktop Background | grep ChangePath | grep -v NewChangePath | tail -n1 | cut -d"=" -f2 | tr '";' ' ' | tr -s ' ' | cut -d" " -f3-`
DIR="$HOME/Pictures/Desktop Pictures"
IMG=`defaults read com.apple.desktop Background | grep LastName | tail -n1 | cut -d"=" -f2 | tr '";' ' ' | cut -d" " -f3`
 
echo "Current Desktop Picture: $IMG"
if [ "$1" = "-rm" ]; then
echo "Are you sure you want to delete $IMG [y/N]: \c"
read result
if [ "x$result" = "xy" -o "x$result" = "xY" ]; then
rm "${DIR}/${IMG}"
echo "Background $IMG deleted"
else
echo "Background $IMG has not been deleted"
fi
fi