Tuesday, September 21, 2021

#1 2021-07-24 01:05:52 pm

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Apple Favorites wallpaper handling

1) Is it possible at all to generate ascript which can pull up EXIF data on the current wallpaper?

My wallpapers are my favorites in Apple Photos, and it'd be great if I could hit a defined function key and find out the location and date the photo was taken.

90% of the time I know the location but......

2) Secondly is it possible to generate a script which will rotate the wallpaper to the next image in the sequence?

Thanks as always for your thoughts.

Offline

 

#2 2021-07-25 08:04:26 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

If it helps this page shows how to get the location of the file being used and write it on top of the desktop picture in use:

https://osxdaily.com/2011/11/20/show-th … -mac-os-x/

Offline

 

#3 2021-07-25 08:09:49 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

Also found this but no ideas how to run it:

https://discussions.apple.com/thread/83 … 3344100022

Offline

 

#4 2021-07-25 08:32:53 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

And this will reportedly extract EXIF data (but doesn't work):

https://macosxautomation.com/applescrip … ts/09.html

So somehow I need to fix the second script, merge it with the first.... and then display the data?

Offline

 

#5 2021-07-25 08:49:46 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

In fact the first script would work if I could open the photo in the Photos app instead of in Finder? I've tried replacing 'Finder' with 'Photo' or 'Photos' to no avail.

Offline

 

#6 2021-07-25 09:29:31 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: Apple Favorites wallpaper handling

One by one

Getting  LOCATION of current desktop picture file:

Applescript:


-- get current Pictures folder
tell application "System Events" to set pFldr to pictures folder of current desktop

-- get relative posix path of picture (in the cash)
set POSIXPath to pFldr & "/" & paragraph 1 of (do shell script "ls -tu " & quoted form of pFldr)

-- get full Posix path of current desktop picture
tell application "Finder" to set pictureFileAsAlias to POSIXPath as POSIX file as alias
set currentPicturePOSIXPath to POSIX path of pictureFileAsAlias

Getting EXPOSURE TIME of current desktop picture file (Note: it will return empty string if the picture file doesn't contain any exposure time info):

1) Install ExifTool on your MAC. It is free and only realable solution on this moment. Adjust script this way:

Applescript:


property exiftoolPath : "/usr/local/bin/exiftool"

-- get current Pictures folder
tell application "System Events" to set pFldr to pictures folder of current desktop

-- get relative posix path of picture (in the cash)
set POSIXPath to pFldr & "/" & paragraph 1 of (do shell script "ls -tu " & quoted form of pFldr)

-- get full Posix path of current desktop picture
tell application "Finder" to set pictureFileAsAlias to POSIXPath as POSIX file as alias
set currentPicturePOSIXPath to POSIX path of pictureFileAsAlias

-- Get ExposureTime tag name and value.
set exposureTime to (do shell script exiftoolPath & " -s3 -ExposureTime " & quoted form of currentPicturePOSIXPath)

Last edited by KniazidisR (2021-07-25 09:53:05 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#7 2021-07-25 09:45:32 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

If I ty to run that script within Keyboard Maestro (run applescript) I get errors, this however does work:

Applescript:

-- https://discussions.apple.com/thread/8373854?answerId=33344100022#33344100022
--
-- Usage: osascript find_wall_pic n

-- n = 1 or 2 (monitor ID)



on replace_chars(this_text, search_string, replacement_string)
   
   -- ref: https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
   
   set AppleScript's text item delimiters to the search_string
   
   set the item_list to every text item of this_text
   
   set AppleScript's text item delimiters to the replacement_string
   
   set this_text to the item_list as string
   
   set AppleScript's text item delimiters to ""
   
   return this_text
   
end replace_chars



on run argv
   
   if (count of argv) < 1 then
       
       set screenId to 1
       
   else
       
       set screenId to item 1 of argv as integer
       
   end if
   
   if screenId ≤ 1 then
       
       set screenId to 1 -- 1st monior index
       
   else
       
       set screenId to 18 -- 2nd monitor index
       
   end if
   
   
   
   -- set a to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.data_id=data.ROWID where preferences.picture_id =" & screenId & "\"") as string
   
   -- set posixaliaspath to "/Users/kangfucius/Library/Caches/com.apple.preference.desktopscreeneffect.desk top/69677504/DSKPhotosRootSource/" & a
   
   
   
   set posixaliaspath to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"select d1.value || '/' || d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.key=10 and pf2.key=16 and pf1.picture_id=" & screenId & "\"") as string
   
   set homepath to (do shell script "echo $HOME")
   
   -- replace "~" in the path to actual $HOME dir
   
   set posixaliaspath to replace_chars(posixaliaspath, "~", homepath)
   
   
   
   set aliaspath to (POSIX file posixaliaspath) as string
   
   set posixpath to POSIX path of aliaspath
   
   set imgfile to POSIX file posixpath
   
   -- tell application "Finder" to reveal imgfile
   
   -- tell application "Finder"
   tell application "Finder"
       
       activate
       
       reveal imgfile
       
   end tell
   
end run

The only issue or what I want is that it does show the file in Finder. Ideally I want the photo to be shown in Photos, or failing that a window to open showing the EXIF data of Location, and date taken

Offline

 

#8 2021-07-25 09:57:28 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: Apple Favorites wallpaper handling

It is not difficult for me to delete my post if it does not interest you.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#9 2021-07-25 09:59:16 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

I didn't say it was of no interest just that it didn't work for me within Keyboards Maestro.

Offline

 

#10 2021-07-25 10:23:50 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

I have installed ExifTool on the Mac and again checked out your script (not using KM) and it does indeed work, and shows the exposure time smile

Looking at the documentation for ExifTool I can't work out how to get it to show location data?

If the script can give out both created date (I got that working) as well as location then I'm very happy!

Hoping you can help...

Offline

 

#11 2021-07-25 10:41:38 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: Apple Favorites wallpaper handling

I have not any photo with GPS info in it. So, add following at end of my script above and test yourself:

Applescript:


-- Get GPS data (location)
set GPSLatitude to (do shell script exiftoolPath & " -s3 -GPSLatitude " & quoted form of currentPicturePOSIXPath)
set GPSLongitude to (do shell script exiftoolPath & " -s3 -GPSLongitude " & quoted form of currentPicturePOSIXPath)

Exists third kind of GPS date, named DateTimeOriginal. You can try to get it this way:

Applescript:


set DateTimeOriginal to (do shell script exiftoolPath & " -s3 -DateTimeOriginal " & quoted form of currentPicturePOSIXPath)

So, all script should be like this (I can't test fully):

Applescript:


property exiftoolPath : "/usr/local/bin/exiftool"

-- get current Pictures folder
tell application "System Events" to set pFldr to pictures folder of current desktop

-- get relative posix path of picture (in the cash)
set POSIXPath to pFldr & "/" & paragraph 1 of (do shell script "ls -tu " & quoted form of pFldr)

-- get full Posix path of current desktop picture
tell application "Finder" to set pictureFileAsAlias to POSIXPath as POSIX file as alias
set currentPicturePOSIXPath to POSIX path of pictureFileAsAlias

-- Get GPS data (location and date)
set DateTimeOriginal to (do shell script exiftoolPath & " -s3 -DateTimeOriginal " & quoted form of currentPicturePOSIXPath)
set GPSLatitude to (do shell script exiftoolPath & " -s3 -GPSLatitude " & quoted form of currentPicturePOSIXPath)
set GPSLongitude to (do shell script exiftoolPath & " -s3 -GPSLongitude " & quoted form of currentPicturePOSIXPath)

Last edited by KniazidisR (2021-07-25 10:54:54 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#12 2021-07-25 11:32:27 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

Okay that makes good progress. smile

The output seems to be the last 'set' line e.g.:

"1 deg 44' 4.87\" W"

Which is from the

Applescript:

set GPSLongitude to (do shell script exiftoolPath & " -s3 -GPSLongitude " & quoted form of currentPicturePOSIXPath)

line. Is there no way of displaying all the output data? Or am I missing something?

The GPSLatitude & Longitude look good, but do not give the location shown in Apple Photos, I guess there is no way to get that?

Offline

 

#13 2021-07-25 09:58:14 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: Apple Favorites wallpaper handling

dbrewood wrote:

Is there no way of displaying all the output data? Or am I missing something?


You can return multiple results  as list:

Applescript:


return {DateTimeOriginal, GPSLatitude, GPSLongitude}

dbrewood wrote:

The GPSLatitude & Longitude look good, but do not give the location shown in Apple Photos, I guess there is no way to get that?


Having GPSLatitude and GPSLongitude you can show location on the map with AsObjC and webview. Somewhere on this site exists example.

Following is one example from Takaaki Naganoya (firstly, you have to convert latitude and longitude from degrees to radians):

Applescript:

-- Created 2019-02-14 by Takaaki Naganoya
-- 2019 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "MapKit"
use framework "AppKit"

property |NSURL| : a reference to current application's |NSURL|
property NSData : a reference to current application's NSData
property MKMapView : a reference to current application's MKMapView
property NSURLRequest : a reference to current application's NSURLRequest
property NSURLConnection : a reference to current application's NSURLConnection

property theResult : 0
property returnCode : 0

set nRes to hasInternetConnection("http://www.google.com") of me
if nRes = false then error "No Internet Connection...."

-- Карта отображает начальные координаты. Oбычно, текущая позиция выбирается и указывается с помощью Assistive-GPS через WiFi.
set aLong to 130.55723797
set aLat to 31.57868838

set paramObj to {myMessage:"Выберите местоположение", mySubMessage:"Пожалуйста, поместите целевое местоположение в центр карты", aLongitude:aLong, aLatitude:aLat}
my performSelectorOnMainThread:"chooseCoordinate:" withObject:(paramObj) waitUntilDone:true
return theResult
--> {latitude:31.57868838, longitude:130.55723797}

on chooseCoordinate:paramObj
   set aMainMes to myMessage of paramObj
   set aSubMes to mySubMessage of paramObj
   set aLong to aLongitude of paramObj
   set aLat to aLatitude of paramObj
   
   -- Создать MKMapView
   set aMapView to MKMapView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 500, 300))
   tell aMapView
       its setMapType:(current application's MKMapTypeStandard)
       its setZoomEnabled:true
       its setScrollEnabled:true
       its setPitchEnabled:true
       its setRotateEnabled:true
       its setShowsCompass:true
       its setShowsZoomControls:true
       its setShowsScale:true
       its setShowsUserLocation:true
       
       set aLocation to current application's CLLocationCoordinate2DMake(aLat, aLong)
       its setCenterCoordinate:aLocation zoomLevel:17 animated:false
       its setDelegate:me
   end tell
   
   -- set up alert    
   set theAlert to current application's NSAlert's alloc()'s init()
   tell theAlert
       its setMessageText:aMainMes
       its setInformativeText:aSubMes
       its addButtonWithTitle:"OK"
       its addButtonWithTitle:"Cancel"
       its setAccessoryView:aMapView
   end tell
   
   -- show alert in modal loop
   current application's NSRunningApplication's currentApplication()'s activateWithOptions:(current application's NSApplicationActivateIgnoringOtherApps)
   set returnCode to theAlert's runModal()
   if returnCode = (current application's NSAlertSecondButtonReturn) then error number -128
   
   set (my theResult) to (aMapView's centerCoordinate()) as record
end chooseCoordinate:

-- Internet Connection Check
on hasInternetConnection(aURLString)
   set aURL to |NSURL|'s alloc()'s initWithString:aURLString
   set aReq to NSURLRequest's alloc()'s initWithURL:aURL cachePolicy:(current application's NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:5.0
   set urlRes to (NSURLConnection's sendSynchronousRequest:aReq returningResponse:(missing value) |error|:(missing value))
   if urlRes = missing value then
       return false
   else
       return true
   end if
end hasInternetConnection

Last edited by KniazidisR (2021-07-25 10:14:42 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#14 2021-07-26 03:41:03 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

Thanks, this is teaching me a lot!

Multiple results showing now so that is great.

Hmm one issue I've just noticed is that even if the image changes on the desktop then the EXIF data returned is always the same so it looks like it's not returning the data for the right picture some how?

The mapping script works so I just need to work out how to convert the lat / long to radians and enter those in the script.

If the lat / log result was in decimal degrees instead of degrees / minutes then that'd be easy I guess. As it'd be decimal degree x 2*pi I think.....

I've asked on the ExifTool forum if there is a way of specifying what unit the tool returns the data in.

Learning curve smile

The help is greatly appreciated.

Last edited by dbrewood (2021-07-26 05:28:52 am)

Offline

 

#15 2021-07-26 07:02:20 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

Phil did come back with:

exiftool -p "${gpslatitude#;$_*=3.14159/180} ${gpslongitude#;$_*=3.14159/180}" FILE

But I can't work out how to revamp that so I can set each into an individual variable?

Offline

 

#16 2021-07-29 01:39:09 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: Apple Favorites wallpaper handling

The example provided by me in the post#13 is not very good. Following is closer to what the OP asked for. To use script the OP should set the aLatitude an aLongitude values. It should be radians. Converting degrees to radians is other task. It is simpler as well. I don't have test photos of the OP to provide converting workaround.

So, following script (adapted from script of Takaaki Naganoya as well) shows location given latitude and longitude of it:

Applescript:


use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "MapKit"
use framework "CoreLocation"

property NSAlert : a reference to current application's NSAlert
property NSString : a reference to current application's NSString
property NSScreen : a reference to current application's NSScreen
property MKMapView : a reference to current application's MKMapView
property MKMapTypeHybrid : a reference to current application's MKMapTypeHybrid
property MKPointAnnotation : a reference to current application's MKPointAnnotation
property MKMapTypeSatellite : a reference to current application's MKMapTypeSatellite
property MKMapTypeStandard : a reference to current application's MKMapTypeStandard
property NSSegmentedControl : a reference to current application's NSSegmentedControl
property NSRunningApplication : a reference to current application's NSRunningApplication
property NSSegmentStyleTexturedRounded : a reference to current application's NSSegmentStyleTexturedRounded
property selSeg : 0
property aMapViewList : {}


set aLatitude to "52.3740"
set aLongitude to "4.8897"
set paramObj to {viewWidth:1000, viewHeight:600, viewSubTitle:"Geolocation Showing Example", viewLat:aLatitude, viewLong:aLongitude}
my performSelectorOnMainThread:"dispMapViewinDifferentScales:" withObject:(paramObj) waitUntilDone:true


on dispMapViewinDifferentScales:paramObj
   set aWidth to (viewWidth of paramObj) as real
   set aHeight to (viewHeight of paramObj) as real
   set aLat to (viewLat of paramObj) as real
   set aLong to (viewLong of paramObj) as real
   --set aTitle to (viewTitle of paramObj) as string
   set aSubTitle to (viewSubTitle of paramObj) as string
   
   set selSeg to 0
   
   --NSViewをつくる
   set aView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   
   --各レベルのMapViewをBoxでつくる
   set wList to {{3, "????World Level Map"}, {5, "????Country Level Map"}, {10, "????City Level Map"}, {17, "????Town Level Map"}}
   set xPos to 0
   repeat with i in wList
       copy i to {aLevelNum, aBoxTitle}
       
       --Boxをつくる
       set aBox to (current application's NSBox's alloc()'s initWithFrame:(current application's NSMakeRect(xPos, 40, aWidth * 0.25, aHeight - 70)))
       (aBox's setTitle:aBoxTitle)
       
       --MapView+Pinをつくる
       set aMapView to makeMKMapView(aWidth * 0.25, aHeight - 70, aLevelNum, aLat, aLong) of me
       
       (aBox's addSubview:aMapView)
       (aView's addSubview:aBox)
       
       set the end of aMapViewList to aMapView
       set xPos to xPos + (aWidth * 0.25)
   end repeat
   
   --Segmented Controlをつくる
   set aSeg to makeSegmentedControl({"Map", "Satellite", "Satellite + Map"}, aWidth, aHeight) of me
   aView's addSubview:aSeg
   
   -- set up alert    
   set theAlert to NSAlert's alloc()'s init()
   tell theAlert
       its setMessageText:""
       its setInformativeText:aSubTitle
       its addButtonWithTitle:"OK"
       its addButtonWithTitle:"Cancel"
       its setAccessoryView:aView
   end tell
   
   -- show alert in modal loop
   NSRunningApplication's currentApplication()'s activateWithOptions:0
   my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
   if (my returnCode as number) = 1001 then error number -128
end dispMapViewinDifferentScales:


on doModal:aParam
   set (my returnCode) to aParam's runModal()
end doModal:



--MKMapViewをつくる
on makeMKMapView(aWidth, aHeight, aZoomLevel, aLat, aLong)
   set aMapView to MKMapView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   aMapView's setMapType:(current application's MKMapTypeStandard)
   
   aMapView's setZoomEnabled:true
   aMapView's setScrollEnabled:true
   aMapView's setPitchEnabled:false
   aMapView's setRotateEnabled:false
   aMapView's setShowsCompass:true
   aMapView's setShowsZoomControls:true
   aMapView's setShowsScale:true
   aMapView's setShowsUserLocation:true
   
   set aLocation to current application's CLLocationCoordinate2DMake(aLat, aLong)
   aMapView's setCenterCoordinate:aLocation zoomLevel:aZoomLevel animated:false
   aMapView's setDelegate:me
   
   --MapにPinを追加
   set anAnnotation to current application's MKPointAnnotation's alloc()'s init()
   anAnnotation's setCoordinate:aLocation
   anAnnotation's setTitle:""
   aMapView's addAnnotation:anAnnotation
   
   return aMapView
end makeMKMapView


--Make Segmented Control
on makeSegmentedControl(titleList, aWidth, aHeight)
   set aLen to length of titleList
   
   set aSeg to NSSegmentedControl's alloc()'s init()
   aSeg's setSegmentCount:aLen
   
   set aCount to 0
   repeat with i in titleList
       set j to contents of i
       (aSeg's setLabel:j forSegment:aCount)
       set aCount to aCount + 1
   end repeat
   
   aSeg's setTranslatesAutoresizingMaskIntoConstraints:false
   aSeg's setSegmentStyle:(NSSegmentStyleTexturedRounded)
   aSeg's setFrame:(current application's NSMakeRect(10, 5, 260, 30))
   aSeg's setTrackingMode:0
   aSeg's setTarget:me
   aSeg's setAction:"clickedSeg:"
   aSeg's setSelectedSegment:0
   
   return aSeg
end makeSegmentedControl

--Segmented Control's clicked event handler
on clickedSeg:aSender
   set aSel to aSender's selectedSegment()
   set selSeg to (aSel + 1)
   set mapList to {MKMapTypeStandard, MKMapTypeSatellite, MKMapTypeHybrid}
   set curMap to contents of item selSeg of mapList
   
   repeat with i in aMapViewList
       set aView to contents of i
       (aView's setMapType:(curMap))
   end repeat
end clickedSeg:

Last edited by KniazidisR (2021-07-29 02:08:46 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#17 2021-07-29 06:35:33 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

That is fantastic! Looks / works really well.

Adding:

Applescript:

-- Created by Hubert
-- https://exiftool.org/forum/index.php?topic=12726.new;topicseen#new
set imageFile to POSIX path of ((choose file) as string)
set gpsCoords to do shell script "usr/local/bin/exiftool " & "'" & imageFile & "'" & " -gpslatitude# -gpslongitude#"
set AppleScript's text item delimiters to ":"
set aLatitude to (text item 2 of paragraph 1 of gpsCoords as number)
set aLongitude to (text item 2 of paragraph 2 of gpsCoords as number)

At the top of the script allows the Latitude / Longitude to be extracted from the selected image and fed through to the rest of the script.

So the only issue to resolve now is finding a working piece of script that can correctly identify the path to the desktop image.

To resolve that I've adapted a script that found the correct patch of the desktop picture and opening it in Finder. This script was:

Applescript:

-- https://discussions.apple.com/thread/8373854?answerId=33344100022#33344100022
--
-- Usage: osascript find_wall_pic n

-- n = 1 or 2 (monitor ID)



on replace_chars(this_text, search_string, replacement_string)
   
   -- ref: https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
   
   set AppleScript's text item delimiters to the search_string
   
   set the item_list to every text item of this_text
   
   set AppleScript's text item delimiters to the replacement_string
   
   set this_text to the item_list as string
   
   set AppleScript's text item delimiters to ""
   
   return this_text
   
end replace_chars



on run argv
   
   if (count of argv) < 1 then
       
       set screenId to 1
       
   else
       
       set screenId to item 1 of argv as integer
       
   end if
   
   if screenId ≤ 1 then
       
       set screenId to 1 -- 1st monior index
       
   else
       
       set screenId to 18 -- 2nd monitor index
       
   end if
   
   
   
   -- set a to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.data_id=data.ROWID where preferences.picture_id =" & screenId & "\"") as string
   
   -- set posixaliaspath to "/Users/kangfucius/Library/Caches/com.apple.preference.desktopscreeneffect.desk top/69677504/DSKPhotosRootSource/" & a
   
   
   
   set posixaliaspath to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"select d1.value || '/' || d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.key=10 and pf2.key=16 and pf1.picture_id=" & screenId & "\"") as string
   
   set homepath to (do shell script "echo $HOME")
   
   -- replace "~" in the path to actual $HOME dir
   
   set posixaliaspath to replace_chars(posixaliaspath, "~", homepath)
   
   
   
   set aliaspath to (POSIX file posixaliaspath) as string
   
   set posixpath to POSIX path of aliaspath
   
   set imgfile to POSIX file posixpath
   
   -- tell application "Finder" to reveal imgfile
   
   -- tell application "Finder"
   tell application "Finder"
       
       activate
       
       reveal imgfile
       
   end tell
   
end run

The original script worked perfectly but if I modify and add that at the start of the script as per:

Applescript:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "MapKit"
use framework "CoreLocation"



-- https://discussions.apple.com/thread/8373854?answerId=33344100022#33344100022
--
-- Usage: osascript find_wall_pic n

-- n = 1 or 2 (monitor ID)



on replace_chars(this_text, search_string, replacement_string)
   
   -- ref: https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
   
   set AppleScript's text item delimiters to the search_string
   
   set the item_list to every text item of this_text
   
   set AppleScript's text item delimiters to the replacement_string
   
   set this_text to the item_list as string
   
   set AppleScript's text item delimiters to ""
   
   return this_text
   
end replace_chars



set screenId to 1


-- set a to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.data_id=data.ROWID where preferences.picture_id =" & screenId & "\"") as string

-- set posixaliaspath1 to "/Users/kangfucius/Library/Caches/com.apple.preference.desktopscreeneffect.desk top/69677504/DSKPhotosRootSource/" & a



set posixaliaspath to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"select d1.value || '/' || d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.key=10 and pf2.key=16 and pf1.picture_id=" & screenId & "\"") as string

set homepath to (do shell script "echo $HOME")

-- replace "~" in the path to actual $HOME dir

set posixaliaspath to replace_chars(posixaliaspath, "~", homepath)



set aliaspath to (POSIX file posixaliaspath) as string

set posixpath to POSIX path of aliaspath

set imgfile to POSIX file posixpath

-- tell application "Finder" to reveal imgfile1

-- tell application "Finder"
-- tell application "Finder"

--    activate

--    reveal imgfile

-- end tell

-- end run


set imageFile to imgfile

-- Created by Hubert =========================================
-- https://exiftool.org/forum/index.php?topic=12726.new;topicseen#new
-- set imageFile to POSIX path of ((choose file) as string)
set gpsCoords to do shell script "usr/local/bin/exiftool " & "'" & imageFile & "'" & " -gpslatitude# -gpslongitude#"
set AppleScript's text item delimiters to ":"
set aLatitude to (text item 2 of paragraph 1 of gpsCoords as number)
set aLongitude to (text item 2 of paragraph 2 of gpsCoords as number)

property NSAlert : a reference to current application's NSAlert
property NSString : a reference to current application's NSString
property NSScreen : a reference to current application's NSScreen
property MKMapView : a reference to current application's MKMapView
property MKMapTypeHybrid : a reference to current application's MKMapTypeHybrid
property MKPointAnnotation : a reference to current application's MKPointAnnotation
property MKMapTypeSatellite : a reference to current application's MKMapTypeSatellite
property MKMapTypeStandard : a reference to current application's MKMapTypeStandard
property NSSegmentedControl : a reference to current application's NSSegmentedControl
property NSRunningApplication : a reference to current application's NSRunningApplication
property NSSegmentStyleTexturedRounded : a reference to current application's NSSegmentStyleTexturedRounded
property selSeg : 0
property aMapViewList : {}


-- set aLatitude to "52.3740"
-- set aLongitude to "4.8897"
set paramObj to {viewWidth:1000, viewHeight:600, viewSubTitle:"Geolocation Showing Example", viewLat:aLatitude, viewLong:aLongitude}
my performSelectorOnMainThread:"dispMapViewinDifferentScales:" withObject:(paramObj) waitUntilDone:true


on dispMapViewinDifferentScales:paramObj
   set aWidth to (viewWidth of paramObj) as real
   set aHeight to (viewHeight of paramObj) as real
   set aLat to (viewLat of paramObj) as real
   set aLong to (viewLong of paramObj) as real
   --set aTitle to (viewTitle of paramObj) as string
   set aSubTitle to (viewSubTitle of paramObj) as string
   
   set selSeg to 0
   
   --NSViewをつくる
   set aView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   
   --各レベルのMapViewをBoxでつくる
   set wList to {{3, "World Level Map"}, {5, "Country Level Map"}, {10, "City Level Map"}, {17, "Town Level Map"}}
   set xPos to 0
   repeat with i in wList
       copy i to {aLevelNum, aBoxTitle}
       
       --Boxをつくる
       set aBox to (current application's NSBox's alloc()'s initWithFrame:(current application's NSMakeRect(xPos, 40, aWidth * 0.25, aHeight - 70)))
       (aBox's setTitle:aBoxTitle)
       
       --MapView+Pinをつくる
       set aMapView to makeMKMapView(aWidth * 0.25, aHeight - 70, aLevelNum, aLat, aLong) of me
       
       (aBox's addSubview:aMapView)
       (aView's addSubview:aBox)
       
       set the end of aMapViewList to aMapView
       set xPos to xPos + (aWidth * 0.25)
   end repeat
   
   --Segmented Controlをつくる
   set aSeg to makeSegmentedControl({"Map", "Satellite", "Satellite + Map"}, aWidth, aHeight) of me
   aView's addSubview:aSeg
   
   -- set up alert
   set theAlert to NSAlert's alloc()'s init()
   tell theAlert
       its setMessageText:""
       its setInformativeText:aSubTitle
       its addButtonWithTitle:"OK"
       its addButtonWithTitle:"Cancel"
       its setAccessoryView:aView
   end tell
   
   -- show alert in modal loop
   NSRunningApplication's currentApplication()'s activateWithOptions:0
   my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
   if (my returnCode as number) = 1001 then error number -128
end dispMapViewinDifferentScales:


on doModal:aParam
   set (my returnCode) to aParam's runModal()
end doModal:



--MKMapViewをつくる
on makeMKMapView(aWidth, aHeight, aZoomLevel, aLat, aLong)
   set aMapView to MKMapView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   aMapView's setMapType:(current application's MKMapTypeStandard)
   
   aMapView's setZoomEnabled:true
   aMapView's setScrollEnabled:true
   aMapView's setPitchEnabled:false
   aMapView's setRotateEnabled:false
   aMapView's setShowsCompass:true
   aMapView's setShowsZoomControls:true
   aMapView's setShowsScale:true
   aMapView's setShowsUserLocation:true
   
   set aLocation to current application's CLLocationCoordinate2DMake(aLat, aLong)
   aMapView's setCenterCoordinate:aLocation zoomLevel:aZoomLevel animated:false
   aMapView's setDelegate:me
   
   --MapにPinを追加
   set anAnnotation to current application's MKPointAnnotation's alloc()'s init()
   anAnnotation's setCoordinate:aLocation
   anAnnotation's setTitle:""
   aMapView's addAnnotation:anAnnotation
   
   return aMapView
end makeMKMapView


--Make Segmented Control
on makeSegmentedControl(titleList, aWidth, aHeight)
   set aLen to length of titleList
   
   set aSeg to NSSegmentedControl's alloc()'s init()
   aSeg's setSegmentCount:aLen
   
   set aCount to 0
   repeat with i in titleList
       set j to contents of i
       (aSeg's setLabel:j forSegment:aCount)
       set aCount to aCount + 1
   end repeat
   
   aSeg's setTranslatesAutoresizingMaskIntoConstraints:false
   aSeg's setSegmentStyle:(NSSegmentStyleTexturedRounded)
   aSeg's setFrame:(current application's NSMakeRect(10, 5, 260, 30))
   aSeg's setTrackingMode:0
   aSeg's setTarget:me
   aSeg's setAction:"clickedSeg:"
   aSeg's setSelectedSegment:0
   
   return aSeg
end makeSegmentedControl

--Segmented Control's clicked event handler
on clickedSeg:aSender
   set aSel to aSender's selectedSegment()
   set selSeg to (aSel + 1)
   set mapList to {MKMapTypeStandard, MKMapTypeSatellite, MKMapTypeHybrid}
   set curMap to contents of item selSeg of mapList
   
   repeat with i in aMapViewList
       set aView to contents of i
       (aView's setMapType:(curMap))
   end repeat
end clickedSeg:

I get error:

error "Error: File not found - Macintosh HD:Users:dbrewood:Pictures:Photos Library.photoslibrary:originals:5:5A65EEE9-15CB-4FA9-8B19-B6FCD42C29C2.jpeg" number 1



So very near to having it working now!

Offline

 

#18 2021-07-29 07:50:32 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: Apple Favorites wallpaper handling

Try following script with your wallpapers.

IMPORTANT: before running the script, make sure the wallpapers will rotated:
                   1) go System Preferences --> Desktop & Screen Saver --> Desktop
                   2) choose folder Favorites of Photos
                   3) click on some concrete photo from this folder.
                   4) set checkbox of Change picture, set it to every minute (for example)

Applescript:


use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "MapKit"
use framework "CoreLocation"

property NSAlert : a reference to current application's NSAlert
property NSString : a reference to current application's NSString
property NSScreen : a reference to current application's NSScreen
property MKMapView : a reference to current application's MKMapView
property MKMapTypeHybrid : a reference to current application's MKMapTypeHybrid
property MKPointAnnotation : a reference to current application's MKPointAnnotation
property MKMapTypeSatellite : a reference to current application's MKMapTypeSatellite
property MKMapTypeStandard : a reference to current application's MKMapTypeStandard
property NSSegmentedControl : a reference to current application's NSSegmentedControl
property NSRunningApplication : a reference to current application's NSRunningApplication
property NSSegmentStyleTexturedRounded : a reference to current application's NSSegmentStyleTexturedRounded
property selSeg : 0
property aMapViewList : {}

-- get current Pictures folder
tell application "System Events" to set pFldr to pictures folder of current desktop

-- get relative posix path of picture (in the cash)
set POSIXPath to pFldr & "/" & paragraph 1 of (do shell script "ls -tu " & quoted form of pFldr)

-- get full Posix path of current desktop picture
tell application "Finder" to set pictureFileAsAlias to POSIXPath as POSIX file as alias
set currentPicturePOSIXPath to POSIX path of pictureFileAsAlias

set gpsCoords to do shell script "usr/local/bin/exiftool " & "'" & currentPicturePOSIXPath & "'" & " -gpslatitude# -gpslongitude#"
set AppleScript's text item delimiters to ":"
set aLatitude to (text item 2 of paragraph 1 of gpsCoords as number)
set aLongitude to (text item 2 of paragraph 2 of gpsCoords as number)
set AppleScript's text item delimiters to ""

set paramObj to {viewWidth:1000, viewHeight:600, viewSubTitle:"Geolocation Showing Example", viewLat:aLatitude, viewLong:aLongitude}
my performSelectorOnMainThread:"dispMapViewinDifferentScales:" withObject:(paramObj) waitUntilDone:true


on dispMapViewinDifferentScales:paramObj
   set aWidth to (viewWidth of paramObj) as real
   set aHeight to (viewHeight of paramObj) as real
   set aLat to (viewLat of paramObj) as real
   set aLong to (viewLong of paramObj) as real
   --set aTitle to (viewTitle of paramObj) as string
   set aSubTitle to (viewSubTitle of paramObj) as string
   
   set selSeg to 0
   
   --NSViewをつくる
   set aView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   
   --各レベルのMapViewをBoxでつくる
   set wList to {{3, "????World Level Map"}, {5, "????Country Level Map"}, {10, "????City Level Map"}, {17, "????Town Level Map"}}
   set xPos to 0
   repeat with i in wList
       copy i to {aLevelNum, aBoxTitle}
       
       --Boxをつくる
       set aBox to (current application's NSBox's alloc()'s initWithFrame:(current application's NSMakeRect(xPos, 40, aWidth * 0.25, aHeight - 70)))
       (aBox's setTitle:aBoxTitle)
       
       --MapView+Pinをつくる
       set aMapView to makeMKMapView(aWidth * 0.25, aHeight - 70, aLevelNum, aLat, aLong) of me
       
       (aBox's addSubview:aMapView)
       (aView's addSubview:aBox)
       
       set the end of aMapViewList to aMapView
       set xPos to xPos + (aWidth * 0.25)
   end repeat
   
   --Segmented Controlをつくる
   set aSeg to makeSegmentedControl({"Map", "Satellite", "Satellite + Map"}, aWidth, aHeight) of me
   aView's addSubview:aSeg
   
   -- set up alert    
   set theAlert to NSAlert's alloc()'s init()
   tell theAlert
       its setMessageText:""
       its setInformativeText:aSubTitle
       its addButtonWithTitle:"OK"
       its addButtonWithTitle:"Cancel"
       its setAccessoryView:aView
   end tell
   
   -- show alert in modal loop
   NSRunningApplication's currentApplication()'s activateWithOptions:0
   my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
   if (my returnCode as number) = 1001 then error number -128
end dispMapViewinDifferentScales:


on doModal:aParam
   set (my returnCode) to aParam's runModal()
end doModal:



--MKMapViewをつくる
on makeMKMapView(aWidth, aHeight, aZoomLevel, aLat, aLong)
   set aMapView to MKMapView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   aMapView's setMapType:(current application's MKMapTypeStandard)
   
   aMapView's setZoomEnabled:true
   aMapView's setScrollEnabled:true
   aMapView's setPitchEnabled:false
   aMapView's setRotateEnabled:false
   aMapView's setShowsCompass:true
   aMapView's setShowsZoomControls:true
   aMapView's setShowsScale:true
   aMapView's setShowsUserLocation:true
   
   set aLocation to current application's CLLocationCoordinate2DMake(aLat, aLong)
   aMapView's setCenterCoordinate:aLocation zoomLevel:aZoomLevel animated:false
   aMapView's setDelegate:me
   
   --MapにPinを追加
   set anAnnotation to current application's MKPointAnnotation's alloc()'s init()
   anAnnotation's setCoordinate:aLocation
   anAnnotation's setTitle:""
   aMapView's addAnnotation:anAnnotation
   
   return aMapView
end makeMKMapView


--Make Segmented Control
on makeSegmentedControl(titleList, aWidth, aHeight)
   set aLen to length of titleList
   
   set aSeg to NSSegmentedControl's alloc()'s init()
   aSeg's setSegmentCount:aLen
   
   set aCount to 0
   repeat with i in titleList
       set j to contents of i
       (aSeg's setLabel:j forSegment:aCount)
       set aCount to aCount + 1
   end repeat
   
   aSeg's setTranslatesAutoresizingMaskIntoConstraints:false
   aSeg's setSegmentStyle:(NSSegmentStyleTexturedRounded)
   aSeg's setFrame:(current application's NSMakeRect(10, 5, 260, 30))
   aSeg's setTrackingMode:0
   aSeg's setTarget:me
   aSeg's setAction:"clickedSeg:"
   aSeg's setSelectedSegment:0
   
   return aSeg
end makeSegmentedControl

--Segmented Control's clicked event handler
on clickedSeg:aSender
   set aSel to aSender's selectedSegment()
   set selSeg to (aSel + 1)
   set mapList to {MKMapTypeStandard, MKMapTypeSatellite, MKMapTypeHybrid}
   set curMap to contents of item selSeg of mapList
   
   repeat with i in aMapViewList
       set aView to contents of i
       (aView's setMapType:(curMap))
   end repeat
end clickedSeg:

Last edited by KniazidisR (2021-07-29 07:52:50 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#19 2021-07-29 08:14:15 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

I agree that looks better, and should work, but.....


Applescript:

-- get full Posix path of current desktop picture
tell application "Finder" to set pictureFileAsAlias to POSIXPath as POSIX file as alias
set currentPicturePOSIXPath to POSIX path of pictureFileAsAlias

This always results in the same location being shown on the map, which is NOT location contained within the photograph in use on the desktop in the rotation which has been set.

The script I posted earlier (see below) does always find the right picture:

Applescript:

-- https://discussions.apple.com/thread/8373854?answerId=33344100022#33344100022
--
-- Usage: osascript find_wall_pic n

-- n = 1 or 2 (monitor ID)



on replace_chars(this_text, search_string, replacement_string)
   
   -- ref: https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
   
   set AppleScript's text item delimiters to the search_string
   
   set the item_list to every text item of this_text
   
   set AppleScript's text item delimiters to the replacement_string
   
   set this_text to the item_list as string
   
   set AppleScript's text item delimiters to ""
   
   return this_text
   
end replace_chars



on run argv
   
   if (count of argv) < 1 then
       
       set screenId to 1
       
   else
       
       set screenId to item 1 of argv as integer
       
   end if
   
   if screenId ≤ 1 then
       
       set screenId to 1 -- 1st monior index
       
   else
       
       set screenId to 18 -- 2nd monitor index
       
   end if
   
   
   
   -- set a to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.data_id=data.ROWID where preferences.picture_id =" & screenId & "\"") as string
   
   -- set posixaliaspath to "/Users/kangfucius/Library/Caches/com.apple.preference.desktopscreeneffect.desk top/69677504/DSKPhotosRootSource/" & a
   
   
   
   set posixaliaspath to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"select d1.value || '/' || d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.key=10 and pf2.key=16 and pf1.picture_id=" & screenId & "\"") as string
   
   set homepath to (do shell script "echo $HOME")
   
   -- replace "~" in the path to actual $HOME dir
   
   set posixaliaspath to replace_chars(posixaliaspath, "~", homepath)
   
   
   
   set aliaspath to (POSIX file posixaliaspath) as string
   
   set posixpath to POSIX path of aliaspath
   
   set imgfile to POSIX file posixpath
   
   -- tell application "Finder" to reveal imgfile
   
   -- tell application "Finder"
   tell application "Finder"
       
       activate
       
       reveal imgfile
       
   end tell
   
end run

this runs within keyboard maestro and it opens a Finder window selecting the correct image file which is set as the desktop picture.

Offline

 

#20 2021-07-29 08:29:59 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

The output from the script I reference is:

Macintosh HD:Users:dbrewood:Pictures:Photos Library.photoslibrary:originals:5:59D5C6A1-E973-4CD8-83DE-B092E0064855.jpeg

Which when passed to Finder opens the picture correctly.

I guess this:

Applescript:

set posixaliaspath to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"select d1.value || '/' || d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.key=10 and pf2.key=16 and pf1.picture_id=" & screenId & "\"") as string

set homepath to (do shell script "echo $HOME")

-- replace "~" in the path to actual $HOME dir

set posixaliaspath to replace_chars(posixaliaspath, "~", homepath)


set aliaspath to (POSIX file posixaliaspath) as string

set posixpath to POSIX path of aliaspath

set imgfile to POSIX file posixpath

Needs changing to generate something like:

Macintosh HD\Users\dbrewood\Pictures\Photos Library.photoslibrary:originals\5:59D5C6A1-E973-4CD8-83DE-B092E0064855.jpeg

I've tried adding in:

Applescript:

set AppleScript's text item delimiters to "\"

to change the : to \ but it gives an error....

Offline

 

#21 2021-07-29 08:30:33 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: Apple Favorites wallpaper handling

I don't use never Keyboard Maestro, but you can try following with it:

Applescript:


use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "MapKit"
use framework "CoreLocation"

property NSAlert : a reference to current application's NSAlert
property NSString : a reference to current application's NSString
property NSScreen : a reference to current application's NSScreen
property MKMapView : a reference to current application's MKMapView
property MKMapTypeHybrid : a reference to current application's MKMapTypeHybrid
property MKPointAnnotation : a reference to current application's MKPointAnnotation
property MKMapTypeSatellite : a reference to current application's MKMapTypeSatellite
property MKMapTypeStandard : a reference to current application's MKMapTypeStandard
property NSSegmentedControl : a reference to current application's NSSegmentedControl
property NSRunningApplication : a reference to current application's NSRunningApplication
property NSSegmentStyleTexturedRounded : a reference to current application's NSSegmentStyleTexturedRounded
property selSeg : 0
property aMapViewList : {}

on run argv
   if (count of argv) < 1 then
       set screenId to 1
   else
       set screenId to item 1 of argv as integer
   end if
   if screenId ≤ 1 then
       set screenId to 1 -- 1st monior index
   else
       set screenId to 18 -- 2nd monitor index
   end if
   
   set posixaliaspath to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"select d1.value || '/' || d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.key=10 and pf2.key=16 and pf1.picture_id=" & screenId & "\"") as string
   set homepath to (do shell script "echo $HOME")
   -- replace "~" in the path to actual $HOME dir
   set posixaliaspath to my replace_chars(posixaliaspath, "~", homepath)
   set aliaspath to (POSIX file posixaliaspath) as string
   set currentPicturePOSIXPath to POSIX path of aliaspath
   
   set gpsCoords to do shell script "usr/local/bin/exiftool " & "'" & currentPicturePOSIXPath & "'" & " -gpslatitude# -gpslongitude#"
   set AppleScript's text item delimiters to ":"
   set aLatitude to (text item 2 of paragraph 1 of gpsCoords as number)
   set aLongitude to (text item 2 of paragraph 2 of gpsCoords as number)
   set AppleScript's text item delimiters to ""
   
   set paramObj to {viewWidth:1000, viewHeight:600, viewSubTitle:"Geolocation Showing Example", viewLat:aLatitude, viewLong:aLongitude}
   my performSelectorOnMainThread:"dispMapViewinDifferentScales:" withObject:(paramObj) waitUntilDone:true
end run

on replace_chars(this_text, search_string, replacement_string)
   -- ref: https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
   set AppleScript's text item delimiters to the search_string
   set the item_list to every text item of this_text
   set AppleScript's text item delimiters to the replacement_string
   set this_text to the item_list as string
   set AppleScript's text item delimiters to ""
   return this_text
end replace_chars

on dispMapViewinDifferentScales:paramObj
   set aWidth to (viewWidth of paramObj) as real
   set aHeight to (viewHeight of paramObj) as real
   set aLat to (viewLat of paramObj) as real
   set aLong to (viewLong of paramObj) as real
   --set aTitle to (viewTitle of paramObj) as string
   set aSubTitle to (viewSubTitle of paramObj) as string
   
   set selSeg to 0
   
   --NSViewをつくる
   set aView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   
   --各レベルのMapViewをBoxでつくる
   set wList to {{3, "????World Level Map"}, {5, "????Country Level Map"}, {10, "????City Level Map"}, {17, "????Town Level Map"}}
   set xPos to 0
   repeat with i in wList
       copy i to {aLevelNum, aBoxTitle}
       
       --Boxをつくる
       set aBox to (current application's NSBox's alloc()'s initWithFrame:(current application's NSMakeRect(xPos, 40, aWidth * 0.25, aHeight - 70)))
       (aBox's setTitle:aBoxTitle)
       
       --MapView+Pinをつくる
       set aMapView to makeMKMapView(aWidth * 0.25, aHeight - 70, aLevelNum, aLat, aLong) of me
       
       (aBox's addSubview:aMapView)
       (aView's addSubview:aBox)
       
       set the end of aMapViewList to aMapView
       set xPos to xPos + (aWidth * 0.25)
   end repeat
   
   --Segmented Controlをつくる
   set aSeg to makeSegmentedControl({"Map", "Satellite", "Satellite + Map"}, aWidth, aHeight) of me
   aView's addSubview:aSeg
   
   -- set up alert    
   set theAlert to NSAlert's alloc()'s init()
   tell theAlert
       its setMessageText:""
       its setInformativeText:aSubTitle
       its addButtonWithTitle:"OK"
       its addButtonWithTitle:"Cancel"
       its setAccessoryView:aView
   end tell
   
   -- show alert in modal loop
   NSRunningApplication's currentApplication()'s activateWithOptions:0
   my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
   if (my returnCode as number) = 1001 then error number -128
end dispMapViewinDifferentScales:


on doModal:aParam
   set (my returnCode) to aParam's runModal()
end doModal:



--MKMapViewをつくる
on makeMKMapView(aWidth, aHeight, aZoomLevel, aLat, aLong)
   set aMapView to MKMapView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   aMapView's setMapType:(current application's MKMapTypeStandard)
   
   aMapView's setZoomEnabled:true
   aMapView's setScrollEnabled:true
   aMapView's setPitchEnabled:false
   aMapView's setRotateEnabled:false
   aMapView's setShowsCompass:true
   aMapView's setShowsZoomControls:true
   aMapView's setShowsScale:true
   aMapView's setShowsUserLocation:true
   
   set aLocation to current application's CLLocationCoordinate2DMake(aLat, aLong)
   aMapView's setCenterCoordinate:aLocation zoomLevel:aZoomLevel animated:false
   aMapView's setDelegate:me
   
   --MapにPinを追加
   set anAnnotation to current application's MKPointAnnotation's alloc()'s init()
   anAnnotation's setCoordinate:aLocation
   anAnnotation's setTitle:""
   aMapView's addAnnotation:anAnnotation
   
   return aMapView
end makeMKMapView


--Make Segmented Control
on makeSegmentedControl(titleList, aWidth, aHeight)
   set aLen to length of titleList
   
   set aSeg to NSSegmentedControl's alloc()'s init()
   aSeg's setSegmentCount:aLen
   
   set aCount to 0
   repeat with i in titleList
       set j to contents of i
       (aSeg's setLabel:j forSegment:aCount)
       set aCount to aCount + 1
   end repeat
   
   aSeg's setTranslatesAutoresizingMaskIntoConstraints:false
   aSeg's setSegmentStyle:(NSSegmentStyleTexturedRounded)
   aSeg's setFrame:(current application's NSMakeRect(10, 5, 260, 30))
   aSeg's setTrackingMode:0
   aSeg's setTarget:me
   aSeg's setAction:"clickedSeg:"
   aSeg's setSelectedSegment:0
   
   return aSeg
end makeSegmentedControl

--Segmented Control's clicked event handler
on clickedSeg:aSender
   set aSel to aSender's selectedSegment()
   set selSeg to (aSel + 1)
   set mapList to {MKMapTypeStandard, MKMapTypeSatellite, MKMapTypeHybrid}
   set curMap to contents of item selSeg of mapList
   
   repeat with i in aMapViewList
       set aView to contents of i
       (aView's setMapType:(curMap))
   end repeat
end clickedSeg:

Last edited by KniazidisR (2021-07-29 08:32:38 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#22 2021-07-29 08:37:16 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

Right this script works:

Applescript:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "MapKit"
use framework "CoreLocation"



-- https://discussions.apple.com/thread/8373854?answerId=33344100022#33344100022
--
-- Usage: osascript find_wall_pic n

-- n = 1 or 2 (monitor ID)



on replace_chars(this_text, search_string, replacement_string)
   
   -- ref: https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
   
   set AppleScript's text item delimiters to the search_string
   
   set the item_list to every text item of this_text
   
   set AppleScript's text item delimiters to the replacement_string
   
   set this_text to the item_list as string
   
   set AppleScript's text item delimiters to ""
   
   return this_text
   
end replace_chars



set screenId to 1


-- set a to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.data_id=data.ROWID where preferences.picture_id =" & screenId & "\"") as string

-- set posixaliaspath1 to "/Users/kangfucius/Library/Caches/com.apple.preference.desktopscreeneffect.desk top/69677504/DSKPhotosRootSource/" & a



set posixaliaspath to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"select d1.value || '/' || d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.key=10 and pf2.key=16 and pf1.picture_id=" & screenId & "\"") as string

set homepath to (do shell script "echo $HOME")

-- replace "~" in the path to actual $HOME dir

set posixaliaspath to replace_chars(posixaliaspath, "~", homepath)


set aliaspath to (POSIX file posixaliaspath) as string

set posixpath to POSIX path of aliaspath

set imgfile to POSIX file posixpath


-- tell application "Finder" to reveal imgfile

-- tell application "Finder"

--    activate

--    reveal imgfile

-- end tell

-- end run


-- set imageFile to imgfile
set imageFile to posixpath

-- Created by Hubert =========================================
-- https://exiftool.org/forum/index.php?topic=12726.new;topicseen#new
-- set imageFile to POSIX path of ((choose file) as string)
set gpsCoords to do shell script "usr/local/bin/exiftool " & "'" & imageFile & "'" & " -gpslatitude# -gpslongitude#"
set AppleScript's text item delimiters to ":"
-- set AppleScript's text item delimiters to "\"
set aLatitude to (text item 2 of paragraph 1 of gpsCoords as number)
set aLongitude to (text item 2 of paragraph 2 of gpsCoords as number)

property NSAlert : a reference to current application's NSAlert
property NSString : a reference to current application's NSString
property NSScreen : a reference to current application's NSScreen
property MKMapView : a reference to current application's MKMapView
property MKMapTypeHybrid : a reference to current application's MKMapTypeHybrid
property MKPointAnnotation : a reference to current application's MKPointAnnotation
property MKMapTypeSatellite : a reference to current application's MKMapTypeSatellite
property MKMapTypeStandard : a reference to current application's MKMapTypeStandard
property NSSegmentedControl : a reference to current application's NSSegmentedControl
property NSRunningApplication : a reference to current application's NSRunningApplication
property NSSegmentStyleTexturedRounded : a reference to current application's NSSegmentStyleTexturedRounded
property selSeg : 0
property aMapViewList : {}


-- set aLatitude to "52.3740"
-- set aLongitude to "4.8897"
set paramObj to {viewWidth:1000, viewHeight:600, viewSubTitle:"Geolocation Showing Example", viewLat:aLatitude, viewLong:aLongitude}
my performSelectorOnMainThread:"dispMapViewinDifferentScales:" withObject:(paramObj) waitUntilDone:true


on dispMapViewinDifferentScales:paramObj
   set aWidth to (viewWidth of paramObj) as real
   set aHeight to (viewHeight of paramObj) as real
   set aLat to (viewLat of paramObj) as real
   set aLong to (viewLong of paramObj) as real
   --set aTitle to (viewTitle of paramObj) as string
   set aSubTitle to (viewSubTitle of paramObj) as string
   
   set selSeg to 0
   
   --NSViewをつくる
   set aView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   
   --各レベルのMapViewをBoxでつくる
   set wList to {{3, "World Level Map"}, {5, "Country Level Map"}, {10, "City Level Map"}, {17, "Town Level Map"}}
   set xPos to 0
   repeat with i in wList
       copy i to {aLevelNum, aBoxTitle}
       
       --Boxをつくる
       set aBox to (current application's NSBox's alloc()'s initWithFrame:(current application's NSMakeRect(xPos, 40, aWidth * 0.25, aHeight - 70)))
       (aBox's setTitle:aBoxTitle)
       
       --MapView+Pinをつくる
       set aMapView to makeMKMapView(aWidth * 0.25, aHeight - 70, aLevelNum, aLat, aLong) of me
       
       (aBox's addSubview:aMapView)
       (aView's addSubview:aBox)
       
       set the end of aMapViewList to aMapView
       set xPos to xPos + (aWidth * 0.25)
   end repeat
   
   --Segmented Controlをつくる
   set aSeg to makeSegmentedControl({"Map", "Satellite", "Satellite + Map"}, aWidth, aHeight) of me
   aView's addSubview:aSeg
   
   -- set up alert
   set theAlert to NSAlert's alloc()'s init()
   tell theAlert
       its setMessageText:""
       its setInformativeText:aSubTitle
       its addButtonWithTitle:"OK"
       its addButtonWithTitle:"Cancel"
       its setAccessoryView:aView
   end tell
   
   -- show alert in modal loop
   NSRunningApplication's currentApplication()'s activateWithOptions:0
   my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
   if (my returnCode as number) = 1001 then error number -128
end dispMapViewinDifferentScales:


on doModal:aParam
   set (my returnCode) to aParam's runModal()
end doModal:



--MKMapViewをつくる
on makeMKMapView(aWidth, aHeight, aZoomLevel, aLat, aLong)
   set aMapView to MKMapView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
   aMapView's setMapType:(current application's MKMapTypeStandard)
   
   aMapView's setZoomEnabled:true
   aMapView's setScrollEnabled:true
   aMapView's setPitchEnabled:false
   aMapView's setRotateEnabled:false
   aMapView's setShowsCompass:true
   aMapView's setShowsZoomControls:true
   aMapView's setShowsScale:true
   aMapView's setShowsUserLocation:true
   
   set aLocation to current application's CLLocationCoordinate2DMake(aLat, aLong)
   aMapView's setCenterCoordinate:aLocation zoomLevel:aZoomLevel animated:false
   aMapView's setDelegate:me
   
   --MapにPinを追加
   set anAnnotation to current application's MKPointAnnotation's alloc()'s init()
   anAnnotation's setCoordinate:aLocation
   anAnnotation's setTitle:""
   aMapView's addAnnotation:anAnnotation
   
   return aMapView
end makeMKMapView


--Make Segmented Control
on makeSegmentedControl(titleList, aWidth, aHeight)
   set aLen to length of titleList
   
   set aSeg to NSSegmentedControl's alloc()'s init()
   aSeg's setSegmentCount:aLen
   
   set aCount to 0
   repeat with i in titleList
       set j to contents of i
       (aSeg's setLabel:j forSegment:aCount)
       set aCount to aCount + 1
   end repeat
   
   aSeg's setTranslatesAutoresizingMaskIntoConstraints:false
   aSeg's setSegmentStyle:(NSSegmentStyleTexturedRounded)
   aSeg's setFrame:(current application's NSMakeRect(10, 5, 260, 30))
   aSeg's setTrackingMode:0
   aSeg's setTarget:me
   aSeg's setAction:"clickedSeg:"
   aSeg's setSelectedSegment:0
   
   return aSeg
end makeSegmentedControl

--Segmented Control's clicked event handler
on clickedSeg:aSender
   set aSel to aSender's selectedSegment()
   set selSeg to (aSel + 1)
   set mapList to {MKMapTypeStandard, MKMapTypeSatellite, MKMapTypeHybrid}
   set curMap to contents of item selSeg of mapList
   
   repeat with i in aMapViewList
       set aView to contents of i
       (aView's setMapType:(curMap))
   end repeat
end clickedSeg:

But half the time I run it I get:

Can’t make text item 2 of paragraph 1 of "" into type number.

Offline

 

#23 2021-07-29 08:40:10 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

KniazidisR wrote:

I don't use never Keyboard Maestro, but you can try following with it:


[/applescript]



If I run that I get the following error, even if I run it as a straight script:


error "Can’t make text item 2 of paragraph 1 of \"\" into type number." number -1700 from text item 2 of paragraph 1 of "" to number

Offline

 

#24 2021-07-29 09:08:40 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2038

Re: Apple Favorites wallpaper handling

It's weird to hear that the script in post #22 works and the script in post #21 doesn't, because they are the same thing. Only my script is more orderly when it comes to properties and handlers. The given error most likely means that the current photo does not contain any GPS data.

You must understand - it is impossible for me to check what you are doing there, because I have neither Keyboard Maestro nor your photos (with or without GPS data). Here you have to figure it out for yourself.

Last edited by KniazidisR (2021-07-29 09:10:35 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#25 2021-07-29 09:18:47 am

dbrewood
Member
From:: Manchester, UK
Registered: 2020-11-30
Posts: 22

Re: Apple Favorites wallpaper handling

It is a strange one indeed. The script you use always returns the same location which is weird.

The other script doesn't. Very strange indeed.

That error is indeed generated when there is no GPS data in the photo smile

The scrips were being run stand alone. But now we have the 'finished' item I've copied it into Keyboard Maestro and it runs perfectly!

So I have a solution. Many, many many thanks for your assistance.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)