Is there a Swift bridge for Python? - swift

I developed a script to set attributes for my iTunes music library on my Mac using an Apple Script bridge called AppScript. AppScript Allowed me to write my code in native Python without having to learn Apple Script. AppSript would translate my native Python to Apple Script. Since Apple Script has been replaced with Swift I am wondering if there is a similar bridge for Swift. I have done my research, but no luck. Additionally if there is, can you provide an example of how to control iTunes(now Music) with said library? Thanks in advance

Swift is designed to use MacOS's programming APIs: AppKit, Quartz, CoreFoundation, NSObject, etc, rather than the higher level OSAX event-driven elements (open, print, close, document, window, etc) used in AppleScript.
The system-bundled python (2.7) comes with pyObjC, which allows python to use the same programming APIs that Swift does, e.g. "writing apps". PyObjC also contains a Scripting Bridge to the AppleScript events and objects. The canonical example code does use iTunes:
from Foundation import *
from ScriptingBridge import *
iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
print iTunes.currentTrack().name()
(Obvs, this is python2 and you need to put brackets round the print command. Also, personally, I wouldn't import * everything, as it's very slow.)
Here are some other methods/attributes based on the Script Dictionary:
iTunes.nextTrack()
iTunes.previousTrack()
iTunes.playpause()
iTunes.fastForward()
iTunes.setShuffleEnabled_(False)
iTunes.currentPlaylist().playOnce_(False)
The system-bundled version of pyObjC is very old, but the library itself is still being developed. If you're using python3, then you should install the latest version of pyObjC.
FWIW, you can actually run uncompiled Swift as a 'script' in the shell.

Related

Cannot find UIDocumentPickerViewController in scope

I am completely new to iOS or Mac development, and I am trying to implement opening and reading files in an app for MacOS. By default I had my app use SwiftUI. Looking up how to implement such a functionality using SwiftUI I saw suggestions to use UIDocumentPickerViewController. However I cannot find a proper documentation as to how to use it in practice. Apple's documentation page is not informative at all -- it doesn't provide any information as of how to actually use this class.
Trying to follow some examples I found elsewhere on the Internet, I am now stuck with getting Cannot find UIDocumentPickerViewController in scope compilation error. I have tried importing UIKit, AppKit, CoreServices, MobileCoreServices, Cocoa but nothing seems to help -- extending the class as described in another StackOverflow answer just fails with the same compilation error.
How do I properly use UIDocumentPickerViewController, or how do I implement the same functionality using some other method if this one is wrong?
Apparently UIDocumentPickerViewController is not available when building for Mac OS X, and NSOpenPanel seems to be a way to get the necessary functionality.

How would I go about writing code that uses android.hardware.automotive.vehicle#2.0 Library?

I'm trying to learn to write Hardware Abstraction Layer (HAL).
Here's the path I've taken so far, please correct me if I'm off in any step.
Downloaded AOSP and built it successfully (86%)
Located Vehicle Hal Support Library
Located android.hardware.automotive.vehicle C++ code.
Things I've attempted after that the steps below without succeeding to get those above classes recognized.
Import android.hardware.automotive.vehicle classes in Android Studio for a typical Android App that targets 29 Api Level.
Adding meta tag of android.car app
Copy/Pasting all source code under AOSP /packages/services/Car/
Partially contemplated adding android.hardware.automotive.vehicle#2.0.so Library and trying to access it through JNI (Not so sure about this one).
Please orient me, I see some repositories on github not doing anything special and somehow they're able to import the package in a java class like this.
import android.hardware.automotive.vehicle.V2_0.VehicleHwKeyInputAction;
import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
import android.hardware.automotive.vehicle.V2_0.VehicleProperty;
import android.hardware.automotive.vehicle.V2_0.VehiclePropertyAccess;
Here
how on earth do they get access to those classes?
Thanks
Vehicle HAL is not meant to be accessed directly from apps. Car Service does that for you.
You have couple options depending on what you're actually trying to accomplish:
Learn to write HAL services - it's like writing a driver for a given hardware (in this case, something that provides car data to Car Service).
Learn to write HAL clients - try modifying EmbeddedKitchenSink app first. Please note you need to build it with AOSP and not in AmdroidStudio since this is a system app (and regular apps doesn't have access to the HAL)
Learn Vehicle APIs - that's what you need car lib for. Details on how to use it: https://stackoverflow.com/a/63321234/14759774

Why doesn't SwiftyZeroMQ support epgm:// and pgm:// transport-classes?

We currently want a way to pass data between devices with sockets so we used ZeroMQ on our server-side with a python script and everything seems to be working. Our protocol address currently is epgm://224.0.0.1:28650, However, we need to communicate with the python script so we decided to use this Swift library. Currently, I'm directly using the library from this repository and it seems to be using ZeroMQ library version 4.2 with patch level .2. Then, using this function, I'm checking whether or not the library has the .pgm protocol which my app reports that there it doesn't support it.
Although the library has this function to check whether or not it supports a certain protocol, I'm unable to find ANYTHING on google regarding how I would get the library to support the pgm protocol.
After looking through the original swift library, I was able to find zmq.h which, from what I can tell is what the swift library is using as it's ZMQ library. So, after discovering this, I attempted to recompile libzmq with the "--with-pgm" build option that we used to fix the issue on PyZMQ. However, I haven't really had much luck with this and I'm not even sure this is how I'm supposed to proceed. I'm currently at a loss and any help would be appreciated. Thanks.
Here's the Swift code that we're currently using:
import SwiftyZeroMQ5
class communicationClass{
var context: SwiftyZeroMQ.Context?;
var subscriber: SwiftyZeroMQ5.SwiftyZeroMQ.Socket?;
init(){
do{
context = try SwiftyZeroMQ.Context();
subscriber = try context?.socket(.subscribe);
try subscriber?.connect("epgm://224.0.0.1:28650");
}
catch{
print("error - \(error)")
}
}
}
Here's the error that it outputs: error - Protocol not supported
Q : "Why doesn't SwiftyZeroMQ support epgm:// and pgm:// transport-classes?"
Documentation is clear & sound on this:The as-is package does explicitly state, it does not support epgm://-transport class as of 2020-10.
The next step:
Try using the pgm://-transport-class, instead of the epgm:// and if that fails, file an issue at the package maintainers' incident-management queue, as they claim it should work.
So, due to the lack of support of EPGM and PGM, we have decided to use raw UDP with ZeroMQ's dish/radio draft apis. These draft apis are supposed to replace EPGM and PGM but they're still in development. The apis work for our use case but it may vary depending on your use case.
EDIT:
So, there's a better answer to this question. The reason why SwiftyZeroMQ (at least my version) doesn't support the EPGM and PGM protocols is that you need to compile ZMQ yourself with the openpgm option.

Include path for XCode6 Beta

Swift has portions of Darwin implemented, for instance, this works:
import Darwin
rand()
Where does XCode6 store the underlying include files for this? I wanted to add in scanf functionality the same way they added in rand().
If you type "rand()" in a Swift notebook, do Jump to Definition in Xcode (CMD-CTRL-J), it will take you to the Swift header for the C standard library.
I believe you can see there all the C standard library functions where Apple has provided a Swift API. But you can't see the Swift implementation, since I don't think that's supplied. I would guess it's compiled into the Swift framework.
But what you asked is where that header file is located. Here's how to find it:
Create a Swift command-line application, and use the rand() function.
Build it.
In the Report Navigator, expand all the underlying command-line build commands, to look for the paths that were passed to the compiler.
If you look at the paths in the link phase, you may find something at /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx
.
But I don't think Swift is using a plaintext file format for the headers, and I think the headers only give you access to C's standard lib because there's also a compiled Swift wrapper layer in between, or some kind of bridging header, so it may not be easy to just add missing functions at this point.

Soundtouch bpm iPhone

I'm trying to integrate a mechanism to calculate the BPM of the song in the iPod library(also on iphone).
Searching on the web I found that the most used and reliable libraries to do this things is soundtouch.Anyone has experience with this library? It is computationally possible to make it run on the iPhone hardware?
I have recently been using the code from the BPMDetect class of the soundtouch library succesfully. Initially compiled it on C++, later on translated the code to C# and lately I've been using the C++ code on an Android app through JNI. I'm not really familiar with development in iOS but I'm almost certain that it is possible what you're trying to do.
The only files you should use from the soundtouch source code are the following:
C++ files
BPMDetect.cpp
FIFOSampleBuffer.cpp
PeakFinder.cpp
Header files
BPMDetect.h
FIFOSampleBuffer.h
FIFOSamplePipe.h
PeakFinder.h
soundtouch_config.h
STTypes.h
At least these are the only ones I had to use to make it work.
The BPMDetect class recieves raw samples through its inputSamples() method, it's capable of calculating a bpm value even when the whole file is not yet loaded into its buffer. I have found that these intermediate values differ from the one obtained once the whole file is loaded, which is more accurate, in my experience.
Hope this helps.
EDIT:
It's a kind of complex process to explain in a comment so I'm going to edit the answer.
The gist of it is that you need your android app to consume native code. In order to do that, you need to compile the files listed above from the soundtouch library with the Android NDK toolset.
That will leave you with native code that will be able to process raw sound data, but you still need to get the data from the sound file, which you can do several ways, I think. The way I was doing it was using the FMOD library for Android, here's a nice example for that: FMOD for Android.
Supposing you declared a method like this in your C code:
void Java_your_package_YourClassName_cPlay(JNIEnv *env, jobject thiz)
{
sound->play();
}
On the Android app you use your native methods in the following way:
public class Sound {
// Native method declaration
private native void cPlay();
public void play()
{
cPlay();
}
}
In order to have a friendlier API to work with you can create wrappers around these function calls.
I put the native C code I was using in a gist here.
Hope this helps.