Disable NSTask Screenshot sound - macos

I am developing a screenshot app and I use NSTask to take the screenshot, But when app take screenshot Mac OSX default screenshout sound will be played.
let theProcess = NSTask()
theProcess.launchPath = "/usr/sbin/screencapture"
theProcess.arguments = ["/Users/profile/Desktop/111.png"]
theProcess.launch()
How can I disable it just for the current task and not generally in the mac osx.
Or It's good if I be able to change the sound.

Pass the -x argument to the command:
theProcess.arguments = ["-x", "/Users/profile/Desktop/111.png"]
More options available by running screencapture --help.

Related

VLCKit-based app prevents screensaver

I've recently built a simple Swift macOS app based upon VLCKit; its purpose is mainly to play IP camera streams via RTSP in a window that stays always visible on screen (i.e. to monitor a gate).
Given the purpose of the app, I keep the streaming playing all the time, except when the user minimizes it in the Dock (meaning that I only call mediaPlayer?.stop in viewDidDisappear).
The app works very well, but I've recently discovered that, unless it is minimized (and, as a consequence, the playback is already stopped), something prevents the screensaver from running.
I've tried subscribing to all NSNotificationCenter com.apple.screensaver.* notifications, and I've realized that when the playback is running, none of them is fired; if I minimize the app and stop the playback, everything behaves normally (screensaver starts after the regular delay, all the com.apple.screensaver notifications are properly detected).
I've also tried running pmset -g to check if my app was listed as preventing sleep, but it's not.
My impression, but I might be wrong, is that my instance of VLCMediaPlayer by default prevents the screensaver launch.
I know that in the VLC Mac app the screensaver can be manually prevented via an advanced setting, but I can't seem to be able to find a parameter to set in my code to tell VLCKit to stop blocking the screensaver.
To your knowledge, by default VLCKit prevents the screensaver from running? Is there a way to alter that behavior?
Please let me know if you need any further detail... and thanks in advance!
In VLC, there are options called "--disable-screensaver" and "--no-disable-screensaver". By default, "--disable-screensaver" is used.
If you want to enable screensaver just do this:
NSArray *options = #[#"--no-disable-screensaver"];
_mediaPlayer1 = [[VLCMediaPlayer alloc] initWithOptions:options];
What else options available in your VLC lib? pass option "--help" to your VLC and it will list all available options.
What is the full list of options? https://wiki.videolan.org/VLC_command-line_help/
It turned out VLCKit does actually prevent the screensaver from running by default, and that it doesn't use libvlc to do so, so the libvlc option "--no-disable-screensaver" I was at one point trying to pass was not respected.
The solution was to comment out a UpdateSystemActivity() function call on line 1409 of VLCMediaPlayer.m, as suggested to me here.

Mac OS X: interacting with an application programmatically

I am working on a project where I need to call methods on an existing application (my own) and use some of its functionality. For e.g. my application ThunderBolt runs on Mac OS X 10.10. It also provides a dictionary of events that can be called externally through Apple Script or some other way that I don't know yet.
My question is what are the different (and better) ways of interacting with an application programmatically on Mac OSX? If I use something like the following code in Apple Script Editor:
tell application "ThunderBolt"
set open_file to (choose file with prompt "Choose the file you wish to parse")
set theContents to read open_file as data
set retPict to (image convert theContents)
end tell
then it is going to launch ThunderBolt with a splash screen and then call "image convert". This can be done via NSAppleScript but still it would launch the application and call methods/events on it.
Is it possible to somehow create an instance of (or get a pointer to) one of the class inside the application and use that? Something similar to COM or automation on a Windows system?
If you're working on OS X 10.10, you might consider taking a look at JavaScript for Automation (JXA).
With it you can apparently build methods into your app that can be invoked from client scripts written in JS (although I'm not yet familiar with the particulars of how to handle implementation of such a thing on the app side). But many of the apps that ship as part of OS X Yosemite have such APIs built in (e.g. iTunes and Finder).
Here's a great tutorial on JXA written by Alex Guyot: http://www.macstories.net/tutorials/getting-started-with-javascript-for-automation-on-yosemite/
The JXA-Cookbook repo also appears to be a nice resource: https://github.com/dtinth/JXA-Cookbook/wiki
Here's a brief example - this script makes iTunes go back one track. Try it while iTunes is playing (by putting the text into Script Editor, with the language option set to JavaScript, and hitting the Run button):
iTunes = Application('iTunes')
state = iTunes.playerState()
// Console msgs show up in the Messages tab of the bottom view:
console.log("playerState: " + state)
iTunes.backTrack()
Alternatively, you can place the code into a .js file and run it on the command line:
$ osascript itunes-backTrack.js
playerState: playing
The way you specify the 'tell application' is the best way, in my opinion.
What do you do with your app that needs to be called? Maybe some of the functionalities can be done with Applescript? It would simplify things a lot.

Lock macbook screen from a sandboxed app

I'm building a mac app that I want to distribute in the mac app store. I need this app to have a lock screen feature.
I have 2 different approaches working, the problem is, as soon as I enable sandboxing for the app (which is required for the mac app store), neither of those approaches will work.
Do you know which entitlement I need to request? Or do you know of a third approach that will work with sandboxing?
Thanks
Approach 1, using CGSession (swift):
var arguments = ["-suspend"]
var task = NSTask()
task.arguments = arguments
task.launchPath = "/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession"
task.launch()
Approach 2, using IORequestIdle (swift):
var r = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler")
if (r > 0) {
IORegistryEntrySetCFProperty(r, "IORequestIdle", kCFBooleanTrue)
IOObjectRelease(r)
}
Sorry to say it can't be done. the purpose of sandboxing is to prevent an app can take the whole computer.
You might try to get a temporary exception thru the channels documented in the sandboxing guide.

Close other applications using swift

Is there a way to close running applications in swift? For instance, if the application I create needs to close safari.
Here's a Swift 5 version for closing running applications without using AppleScript (AppleScript is a perfect way but it isn't the only way), Safari is used as the example in this case:
let runningApplications = NSWorkspace.shared.runningApplications
if let safari = runningApplications.first(where: { (application) in
return application.bundleIdentifier == "com.apple.Safari" && application.bundleURL == URL(fileURLWithPath: NSWorkspace.shared.fullPath(forApplication: "Safari")!)
}) {
// option 1
safari.terminate()
// option 2
kill(safari.processIdentifier, SIGTERM)
}
SIGTERM instead of SIGKILL, referencing from here
Of course, make sure you notify the user of this activity since this may cause negative impact on the user-experience (for example, user-generated contents in the targeted application are not saved before terminating)
It is certainly possible via an applescript directly IF:
your app is not running sandboxed (note that if you plan to distribute it via the App Store, your app will be sandboxed)
OR
your app has the necessary entitlements to use applescript: com.apple.security.scripting-targets (apple needs to approve that AND you need to know which apps to target. this isn't a blanket permission)
then
https://apple.stackexchange.com/questions/60401/how-do-i-create-an-applescript-that-will-quit-an-application-at-a-specific-time
Can you execute an Applescript script from a Swift Application
if you aren't going for App Store complicity anyways, you might also use NSTask directly
scripts / code snippets:
How to force kill another application in cocoa Mac OS X 10.5
Can you execute an Applescript script from a Swift Application
short & sweet: technically yes, 'politically' maybe :D

Is there another way to make a screenshot of background windows apart from screencapture -l?

I'd like to take a screenshot of a background window given its title.
I know this can be made with screencapture -l idwindow output but unluckily I can't use this method because I'm on a Mac OS X 10.5.8 system so the command screencapture doesn't support the option -l.
Are there other ways to achieve this?
Thanks in advance.
GrabFS looks like a nifty tool that can solve your problem. It basically turns running programs into a file system like structure that lets you grab screenshots of those running programs.
Requires Mac OS X Leopard and MacFUSE. You are on 10.5.8 (Leopard?) so that should work out OK.

Resources