Save Windows Spotlight photos

Share your advanced PureBasic knowledge/code with the community.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Save Windows Spotlight photos

Post by Little John »

Windows 10 includes the Spotlight feature, which shows very beautiful photos from various locations in the world. The following short code copies all Windows Spotlight photos to a directory of your choice.
On my PC, I have compiled the code to an EXE program, and have put a link to it into Windows' Autostart folder. So on every startup, all new Spotlight photos are automatically saved to the destination directory. Enjoy your growing collection of those pleasing photos! :D

Code: Select all

; >> Executable format: Console
; tested with PB 5.44 LTS on Windows 10

EnableExplicit


Procedure.i IsJpegFile (file$)
   ; out: 1 = yes
   ;      0 = no
   ;     -1 = unknown
   Protected ifn.i, firstWord.u, ret.i
   
   ifn = ReadFile(#PB_Any, file$)
   If ifn = 0
      ProcedureReturn -1   ; error
   EndIf
   
   firstWord = ReadWord(ifn)
   If firstWord = $D8FF
      ret = 1
   Else   
      ret = 0
   EndIf
   CloseFile(ifn)
   
   ProcedureReturn ret
EndProcedure


Procedure.i CopySpotlightPhotos (targetDir$)
   ; in : full name of the target directory
   ; out: 1 on success,
   ;      0 on error
   Protected sourceDir$, sourceFile$, targetFile$, console.i, dir.i, count.i=0
   
   If FileSize(targetDir$) <> -2         ; if target directory does not exist
      ProcedureReturn 0                  ; error
   EndIf
   
   sourceDir$ = GetHomeDirectory() + "AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets"
   
   console = OpenConsole()
   
   dir = ExamineDirectory(#PB_Any, sourceDir$, "*.*")
   If dir   
      While NextDirectoryEntry(dir)
         If DirectoryEntryType(dir) = #PB_DirectoryEntry_File And
            DirectoryEntrySize(dir) > 50000                             ; Small files in that directory are not Spotlight photos.
            sourceFile$ = sourceDir$ + "\" + DirectoryEntryName(dir)
            If IsJpegFile(sourceFile$) = 1
               targetFile$ = targetDir$ + "\" + DirectoryEntryName(dir) + ".jpg"
               CopyFile(sourceFile$, targetFile$)
               count + 1 
               If console
                  PrintN(targetFile$)
               EndIf
            EndIf
         EndIf
      Wend   
      FinishDirectory(dir)
   EndIf
   
   If console
      PrintN("")
      Print("Copied " + count + " files.")
      Delay(2000)
      CloseConsole()
   EndIf
   
   ProcedureReturn 1          ; success
EndProcedure


Define targetDir$ = GetHomeDirectory() + "Pictures\WindowsSpotlight"

If CopySpotlightPhotos(targetDir$) = 0
   MessageRequester("Error", "Target directory '" + targetDir$ + "' not found.")
EndIf
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Save Windows Spotlight photos

Post by davido »

Hi Little John,

I wondered if it might be possible to save these pictures.
Well, now I know!

Thank you, very much.
DE AA EB
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Save Windows Spotlight photos

Post by Little John »

Hi davido,

thanks!
I'm glad that the code is useful for you.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Save Windows Spotlight photos

Post by Fangbeast »

Me too Little John. I had forgotten those were stored somewhere. Quite pretty too.
Amateur Radio, D-STAR/VK3HAF
juror
Enthusiast
Enthusiast
Posts: 228
Joined: Mon Jul 09, 2007 4:47 pm
Location: Courthouse

Re: Save Windows Spotlight photos

Post by juror »

Wonderful. Love those pics and was wondering if there was a way to capture.

Many thanks.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Save Windows Spotlight photos

Post by Little John »

Hi Fangbeast and juror,

you are welcome.
Nice to read that you like those pictures, too.
Post Reply