"tell application" in XCode [closed] - xcode

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Very very newbie to XCode here, although I used it years ago to do all sorts of things in what they used to call Applescript Studio.
I'm trying to get the Finder to tell me the name of the file dropped on my app:
on |application|:theApp openFile:aFile
display alert "123"
tell application "Finder"
set theName to (name of aFile)
end tell
display alert theName
end
"123" is displayed, but theName is not. Surmising that my syntax is wrong -- I've taken a look at Scripting Bridge, but this seems to be about making my app be able to respond to apple events from other scripts, which is not what I want.
My apologies for straying into possible off-topic areas with my original post. I figured that it was better to find somewhere that I can solve my own problems than to have to come running for help every time I need it ;-)
Can someone please help me figure out how to get the Finder to tell me the name of a file?

aFile is an NSString, so you can use Cocoa to parse aFile itself. You have the full resources of Cocoa at your beck and call; involving the Finder is unnecessary. But you cannot display an NSString using AppleScript; you must cast it to an AppleScript string first. So, for example:
set lpc to aFile's lastPathComponent()
display alert (lpc as string)
If you must use AppleScript to do the parsing (e.g. asking the Finder to do it for you) because you're more comfortable that way, then just say aFile as string up front and then go ahead and parse further the resulting AppleScript string. For example:
set p to aFile as string
tell application "Finder"
set f to POSIX file p as alias
get name of f
display alert result
end tell
However, I think that's a very foolish approach; scripting another app is expensive, while using Cocoa directly is not.

Related

How to change the wallpaper for all spaces in macOS using Applescript/shell?

Before I've written this question, I've reviewed several answers and the most popular ones was
tell application "System Events"
tell every desktop
set picture to "/Users/xyz/Documents/Screenshots/wallpaper.png"
end tell
end tell
But it doesn't seem to be working.
What I've tried?
Steps:
Make sure have more than one space
Run the script
Expected result: All spaces should see new wallpaper
Actual result: Wallpaper is changed to only current space
Goal: Is there a way to change the wallpaper to all the spaces using applescript/shell?

Get path to original item of a file alias when it's missing

I'm trying to write a script to correct the path to an original item of a file alias. I know what's wrong with the path (I moved a folder), so if I can get what the system thinks the path is, I can fix it.
The problem is that the script simply reports that the original can't be found and not where the original should be.
Is there a way to grab that from with AppleScript? It's there in the Get Info... box, so it's stored in the file. I suspect I could use some bash way to get it with a do shell script, but I'm curious about staying within AS.
Years ago, I also had issue with my alias, when changing folders. This is part of the script I made which looks into Get Info window. Since, may be the Get Info window has been changed on last OS version, therefore, some adjustments are required, but it gives you the direction to go:
on Origine_Alias(F_Alias)
set R to ""
tell application "Finder" to open information window of file (F_Alias as text)
tell application "System Events" to tell process "Finder" to set R to value of static text 18 of scroll area 1 of front window
tell application "Finder" to close front window
return R
end Origine_Alias
The returned string is in Unix format: /folder/my/file

Setting a custom label to a file with AppleScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here's what I got, but I ave a custom label with "Active Projects".
How do I assign?
tell application "Finder" to set label index of theFile to 5
You can't do that with vanilla AppleScript. The Finder dictionary does not support adding tags.
However you can do it with AppleScriptObjC which gives access to the Foundation framework
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
on addTagToPath(theTag, thePath)
set theURL to current application's NSURL's fileURLWithPath:thePath
set {success, tagArray, theError} to theURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(reference)
if theError is not missing value then error theError's localizedDescription() as text
if tagArray is not missing value and (tagArray's containsObject:theTag) as boolean is true then return
if tagArray is missing value then set tagArray to current application's NSMutableArray's array()
tagArray's addObject:theTag
set {success, theError} to theURL's setResourceValue:tagArray forKey:(current application's NSURLTagNamesKey) |error|:(reference)
if theError is not missing value then error theError's localizedDescription() as text
end addTagToPath
and use it
try
addTagToPath("MyTag", "/Users/myUser/path/to/file.ext")
on error e
log e
end try
The try block catches errors thrown by the NSURL methods

How to browse Applescript object "DOM" for a given object?

Given an object in Applescript (i.e. I have an Object Specifier to it), how do I find out what all accessible properties(?) that it has are? I am not sure that properties is the right word here, and part of my question is what the correct vocabulary is; I will illustrate what I mean with the following example.
For instance:
Using the following Object Specifier, tell application "System Events" to get process "TextEdit", I can access the window property(?) of it, such as tell application "System Events" to get window of process "TextEdit". (Note that window is a list, apparently).
What I want is to query the Object Specifier in some way that will show me which things (properties?), like window, are available in the object. In Python, for example, this could be done using dir(object_name). Is there an equivalent in Applescript?
I've tried the following two approaches but these, while providing useful lists, do not achieve the goal since they do not return window like desired in the example.
tell application "System Events" to get properties of process "TextEdit"
tell application "System Events" to get attributes of process "TextEdit"
I am looking for something in a similar style which would return window and the other properties(?) like window. How can I call such a query in Applescript code?
Also, what precisely is the window of process "TextEdit" here? Is it a property? An attribute? Something else? Is there an easy way to answer that question within the Applescript Editor, such as perhaps by interpreting the blue-italics display of the returned window object? Does that appearance have significance for distinguishing between properties, attributes, etc? (What does 'etc' include here, specifically and using correct vocabulary, is part of my question..)
In AppleScript
Properties are leaf items like name, identifier.
Elements are node items like attributes, windows, documents.
However element is not a part of the standard AppleScript terminology.
If the application is scriptable (which TextEdit is) you get a scripting dictionary to access the application elements and properties.
If the application is not scriptable you can access the UI properties and elements of the application process via System Events.
The AppleScript dictionary of an application contains all information about the available elements, properties, commands / events, enumerations and value types. In Script Editor press ⇧⌘O and select the dictionary.
A more sophisticated – and expensive – tool is Script Debugger which provides a terminology browser. For professional script developing it's indispensable.

Need an Applescript to go to a specific slide number in PowerPoint

I need a simple AppleScript that will cause PowerPoint to move to a specified slide number. I unfortunately am not familiar enough with AppleScript to properly interpret the information in the Applescript Library for PowerPoint. I think if someone can get me started I can take it from there.
The script just needs to reference the active presentation. If I could speak to my computer I just need to tell it to "go to slide #5" or "go to slide #2", etc.
The script needs to work while in Slideshow mode as the presentation will be running on a separate screen. Thanks so much for the help, I haven't been able to find this anywhere.
my gotoSlide(2)
on gotoSlide(whichSlide)
-- whichSlide is the integer position of the slide
tell application "Microsoft PowerPoint"
activate
tell active presentation
set theView to view of document window 1
go to slide theView number whichSlide
end tell
end tell
end gotoSlide

Resources