Thursday, February 25, 2021

#1 2021-01-22 05:54:54 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Random deskop picture

I thought this was simple but the last line return error...

What I'm missing??...

Applescript:

use framework "Foundation"
use framework "AppKit"
use framework "GameplayKit"
use scripting additions

set thePath to POSIX path of (path to desktop pictures folder)
set anArray to (current application's NSFileManager's defaultManager())'s contentsOfDirectoryAtPath:thePath |error|:(missing value)
set randomSource to current application's GKRandomSource's alloc()'s init()
set shuffle to randomSource's arrayByShufflingObjectsInArray:anArray
set randomObject to (shuffle's objectAtIndex:0) as text

set theURL to current application's |NSURL|'s fileURLWithPath:(thePath & randomObject)

set workspace to current application's NSWorkspace's sharedWorkspace()

set screen to current application's NSScreen's mainScreen()
set theResult to workspace's setDeskopImageURL:theURL forScreen:screen options:0 |error|:(missing value)

Today, I was able to make ASObjC to work, the options:0 -> options:(missing value), I also put
everything in 1 line of code. Instead of using variable for NSWorkspace and NSScreen.

hmmm....

Applescript:

use framework "Foundation"
use framework "AppKit"
use framework "GameplayKit"
use scripting additions

set thePath to POSIX path of (path to desktop pictures folder)
set anArray to (current application's NSFileManager's defaultManager())'s contentsOfDirectoryAtPath:thePath |error|:(missing value)
set randomSource to current application's GKRandomSource's alloc()'s init()
set shuffle to randomSource's arrayByShufflingObjectsInArray:anArray
set randomObject to (shuffle's objectAtIndex:0) as text

set theURL to current application's |NSURL|'s fileURLWithPath:(thePath & randomObject)

set {theResult, theError} to ((current application's NSWorkspace's sharedWorkspace())'s setDesktopImageURL:theURL forScreen:(current application's NSScreen's mainScreen()) options:(missing value) |error|:(reference))

At least this PyObjC do work.

import glob
import random

from AppKit import (NSWorkspace, NSScreen)
from Foundation import NSURL

theGlob = glob.glob('/Library/Desktop Pictures/*.jpg')
thePath = random.choice(theGlob)

theURL = NSURL.fileURLWithPath_(thePath)

workspace = NSWorkspace.sharedWorkspace()

for screen in NSScreen.screens():
    (result, error) = workspace.setDesktopImageURL_forScreen_options_error_(theURL,screen,None,None)

Last edited by Fredrik71 (2021-01-23 04:17:17 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#2 2021-01-25 11:53:43 pm

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

Re: Random deskop picture

Fredrik71 wrote:

I thought this was simple but the last line return error...

What I'm missing??...


Creation the NS dictionary. Because options of setDesktopImageURL method are of dictionary type {key1:value1, key2:value2...}. So:

Applescript:


use framework "Foundation"
use framework "AppKit"
use framework "GameplayKit"
use scripting additions

property NSFileManager : a reference to NSFileManager of current application
property NSImageScaling : a reference to NSImageScaling of current application
property NSScreen : a reference to NSScreen of current application
property GKRandomSource : a reference to GKRandomSource of current application
property |NSURL| : a reference to |NSURL| of current application
property NSWorkspace : a reference to NSWorkspace of current application
property NSNumber : a reference to current application's NSNumber
property NSDictionary : a reference to NSDictionary of current application
property NSWorkspaceDesktopImageScalingKey : a reference to current application's NSWorkspaceDesktopImageScalingKey

set thePath to POSIX path of (path to desktop pictures folder)
set anArray to (NSFileManager's defaultManager())'s contentsOfDirectoryAtPath:thePath |error|:(missing value)
set randomSource to GKRandomSource's alloc()'s init()
set shuffle to randomSource's arrayByShufflingObjectsInArray:anArray
set randomObject to (shuffle's objectAtIndex:0) as text

set theURL to |NSURL|'s fileURLWithPath:(thePath & randomObject)
set workspace to NSWorkspace's sharedWorkspace()
set screen to NSScreen's mainScreen()

set aNum to NSNumber's numberWithInt:0
set aDictionary to NSDictionary's dictionaryWithObject:aNum forKey:NSWorkspaceDesktopImageScalingKey

(workspace's setDesktopImageURL:theURL forScreen:screen options:aDictionary |error|:(missing value))

Last edited by KniazidisR (2021-01-25 11:55:33 pm)


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

Offline

 

#3 2021-01-28 05:52:20 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Re: Random deskop picture

Thanks, from your code I made a change... if options:(missing value)

Applescript:

set theNull to current application's NSNull's |null|()
set aDictionary to NSDictionary's dictionaryWithObject:theNull forKey:(current application's NSWorkspaceDesktopImageOptionKey)
(workspace's setDesktopImageURL:theURL forScreen:screen options:aDictionary |error|:(missing value))


The purpose to study someone else art is not to add, its to make less more.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)